Omron Forums Support Posted March 14, 2022 Share Posted March 14, 2022 The client application is listening on a single wensocket. On every trigger, the reader will send automatically a report result to the client. The report data is sent as raw XML data. Parsing of this data is not included in this example.1. Create a new project of Windows Forms in VisualStudio. (It is not a must to use Windows Form, but for the sake of this example it will be easier. This example is done with VisualStudio2015.) 2. Add to the project Websocket4Net.dll as reference (download it from this link in case you don’t have it: www.websocket4net.codeplex.com). 3. In the source file Form1.cs, add the required reference: using WebSocket4Net; 4. Add the required controls to your Windows Form (buttons, textbox…). See attached project file for a complete example. Open Websocket ConnectionIn order to open a Websocket connection, you need to create an instance of WebSocket, giving the correct url for opening a Websocket connection with the reader as a server: websocket = new WebSocket("ws://192.168.188.2:50502/"); In the line above, WebSocket instance is created with the name websocket, with a reader IP address 192.168.188.2 which is the default IP address of the reader. The number 50502 is the Websocket port number. This port is used for getting report results only. In order to control the reader, use port 50501. (See separate code example about it in this knowledge base.)Define Event HandlersThe WebSocket class will raise events on specific actions like connection opened, data received, etc. In order to be able to handle such events, we need to add them first, declaring the names of the corresponding event handlers: Event handler for connection opened:websocket.Opened += new EventHandler(websocket_Opened); Event handler for connection closed: websocket.Closed += new EventHandler(websocket_Closed); Event handler for receiving data:websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived); Handler functions:Now we also need to define the handler functions for each one of the above events. The handler function will be called when the corresponding event rises. (Log can be a simple function to display text string as user output).Handler function for connection opened event: private void websocket_Opened(object sender, EventArgs e){ Log("Websocket opened \r\n");} Handler function for connection closed event: private void websocket_Closed(object sender, EventArgs e){ Log("Websocket closed \r\n");} Handler function for message received event: private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e){ Log(e.Message);} After defining the event handler functions, when the Websocket is opened it will launch ‘Websocket opened’ event. When data is received from the reader (=server) over this Websocket, it will launch the event ‘Websocket message received’.Receive Data from the ReaderAs explained above, when data is received from the reader, the event ‘Websocket message received’ is launched. Now all we have left to do is define the code inside the handler function websocket_MessageReceived to perform the desired functionality. In the example above, the received text is simply fed into Log function which simply displays the message as output.Closing the Websocket ConnectionTo close the websocket connection with the reader, use the following method: websocket.Close(); As mentioned above, when the Websocket connection is closed, it will launch the ‘Websocket closed’ event.For more info please refer to the documentation of Websocket4Net library. In this document we show a minimal code example in order to get started. Operating System Any Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Top Posters In This Topic
1
Popular Days
Mar 14
1
Top Posters In This Topic
Omron Forums Support 1 post
Popular Days
Mar 14 2022
1 post