1

I am currently trying to feed my socket.io server with data from my C# client. But I am not sure how to receive the message on the server.

My server code:

const io = require('socket.io')(9000);

io.on('connection', (socket) => {
     console.log('Connected');
}

First of all I don't know which event I have to listen to, but nevertheless I am unable to send data to my server using the following client (which uses Websocket-sharp) code:

private void init()
        {
            // start socket connection
            using (var ws = new WebSocket("ws://localhost:9000/socket.io/?EIO=2&transport=websocket"))
            {
                ws.OnMessage += (sender, e) =>
                    API.consoleOutput("Message: " + e.Data);

                ws.OnError += (sender, e) =>
                    API.consoleOutput("Error: " + e.Message);

                ws.Connect();
                ws.Send("server");
            }
        }

The connection works, but how do I receive the message of the server? The sending does not fire an error, therefore I think it does work.

4
  • I think, your connection is not working, because socket.io doesn't uses websocket protocol exactly. So, c# websocket client can't communicate with node.js socket.io server. Probably, you can try github.com/Quobject/SocketIoClientDotNet for c# socket.io client. Commented Jan 6, 2017 at 14:05
  • haven't found that one. Only an outdated one. Will try that later. Commented Jan 6, 2017 at 14:07
  • I ain't sure about the maintenance of SocketIoClientDotNet, but I can say for sure that websocket-sharp can't be directly used to communicate with socket.io server. github.com/sta/websocket-sharp/issues/81#issuecomment-104650660 Commented Jan 6, 2017 at 14:10
  • 1
    SocketIoClientDotNet keeps reconnecting and does not fire the local Socket.EVENT_CONNECT event. Seems to be not working either. Commented Jan 6, 2017 at 14:18

1 Answer 1

1

I've gotten this working for a UWP app that connects to a node.js server. Basically what I do is connect to a URL that looks like ws://localhost:4200/socket.io/?EIO=3&transport=websocket

the port number being something we chose.

once that is set I connect to the node.js socket io library via the following lines of code.

private async Task ConnectWebsocket() {
        websocket = new MessageWebSocket();
        Uri server = new Uri(WebSocketURI); //like ws://localhost:4300/socket.io/?EIO=3&transport=websocket

        websocket.Control.MessageType = SocketMessageType.Utf8;
        websocket.MessageReceived += Websocket_MessageReceived;
        websocket.Closed += Websocket_Closed;
        try {
            await websocket.ConnectAsync(server);
            isConnected = true;
            writer = new DataWriter(websocket.OutputStream);
        }
        catch ( Exception ex ) // For debugging
        {
            // Error happened during connect operation.
            websocket.Dispose();
            websocket = null;
            Debug.Log("[SocketIOComponent] " + ex.Message);

            if ( ex is COMException ) {
                Debug.Log("Send Event to User To tell them we are unable to connect to Pi");
            }
            return;
        }
    }

`

at this point your socket io on "connection" should fire on your server

then you can emit events to it like normal. except the C# socket code does not discriminate various channels so you must do so on your own. below is how we do it (aka SocketData and SocketIOEvent are classes we have defined)

private void Websocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) {
        try {
            using ( DataReader reader = args.GetDataReader() ) {
                reader.UnicodeEncoding = UnicodeEncoding.Utf8;

                try {
                    string read = reader.ReadString(reader.UnconsumedBufferLength);
                    //read = Regex.Unescape(read);
                    SocketData socc = SocketData.ParseFromString(read);
                    if (socc != null ) {
                        Debug.Log(socc.ToString());
                        SocketIOEvent e = new SocketIOEvent(socc.channel, new JSONObject( socc.jsonPayload));
                        lock ( eventQueueLock ) { eventQueue.Enqueue(e); }
                    }
                }
                catch ( Exception ex ) {
                    Debug.Log(ex.Message);
                }
            }
        } catch (Exception ex ) {
            Debug.Log(ex.Message);
        }

    }

in our specific application we did not need to send messages to our server, so for that I do not have a good answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.