0

I'm trying to use a C# Socket.IO implementation for IPC with my Node server.

I looked up a few examples online and the Socket.IO seems to establish, however it's not triggering events.

I was using this article for the basic example that had been confirmed working but haven't had much luck. (websocket-sharp how to work with socketio server and send "emit" request)

I've tried to debug a working Node - Node / Client - Server implementation to see where this might be going wrong but I honestly can't tell. Also the post request I found on this answer here: Communicating with a socket.io server via c# so I first thought it was required to use a post request and in a Wireshark capture I can see a get request, though I think that may just be part of the io engine..

I can see that data is being received by the server but when I do a debug on the Node server I get the following:


listening on port: 4444
  express:router use '/' query +107ms
  express:router:layer new '/' +0ms
  express:router use '/' expressInit +2ms
  express:router:layer new '/' +0ms
  express:router use '/' jsonParser +0ms
  express:router:layer new '/' +1ms
  express:router use '/' urlencodedParser +1ms
  express:router:layer new '/' +0ms
  express:router:route new '/' +1ms
  express:router:layer new '/' +0ms
  express:router:route post '/' +0ms
  express:router:layer new '/' +0ms
  engine handshaking client "peXO2SOcvKhmEotXAAAA" +0ms
  engine:socket sending packet "open" ({"sid":"peXO2SOcvKhmEotXAAAA","upgrades":[],"pingInterval":25000,"pingTimeout":5000}) +0ms
  engine:socket flushing buffer to transport +1ms
  engine:ws writing "0{"sid":"peXO2SOcvKhmEotXAAAA","upgrades":[],"pingInterval":25000,"pingTimeout":5000}" +0ms
  engine:transport setting request +0ms
  socket.io:server incoming connection with id peXO2SOcvKhmEotXAAAA +13s
  engine:ws received "42["connection"]" +45ms
  engine:socket packet +48ms
  socket.io-parser decoded 2["connection"] as {"type":2,"nsp":"/","data":["connection"]} +0ms
  socket.io:client no socket for namespace / +0ms
  engine:ws received "42["init","Hello"]" +129ms
  engine:socket packet +127ms
  socket.io-parser decoded 2["init","Hello"] as {"type":2,"nsp":"/","data":["init","Hello"]} +126ms
  socket.io:client no socket for namespace / +125ms
  engine:socket writing ping packet - expecting pong within 5000ms +25s
  engine:socket sending packet "ping" (undefined) +1ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "2" +25s
  engine:ws received "42["init","Hello"]" +5ms
  engine:socket packet +5ms
  socket.io-parser decoded 2["init","Hello"] as {"type":2,"nsp":"/","data":["init","Hello"]} +25s
  socket.io:client no socket for namespace / +25s

This is my server code:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server),
    util = require('util'),
    bodyParser = require('body-parser');

var port = process.env.port || 4444;
server.listen(port);
console.log("listening on port: " + port);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.post("/", function (req, res) {
    // look for parameters from form input
    var data = {
        ID: req.body.ID,
    };
    console.log("sending request to interperet new data");
    console.log(req.body);

    // send the message to console app

    io.emit("test",data);
});

io.on('connection', ( socket ) =>  {
        console.log("New user joined");

        socket.on('connect', () => {
                console.log("New user joined");
                setTimeout(() => {
                console.log("Emitting test...");
                io.emit("test", data);
                res.send("OK");
                }, 1500);
        });
});

And my C# code (if that helps):

var ws = new WebSocket ("ws://<IP>:4444/socket.io/?EIO=2&transport=websocket");
ws.OnOpen += (sender, e) => {
  ws.Send("42[\"connection\", {\"dummy\":\"data\"}]");
};

ws.Connect ();


Any help getting my head around this would be much appreciated

orgg
  • 97
  • 1
  • 9

1 Answers1

0

For anyone having this issue... I have managed to fix this after I did some reading and found What do these numbers mean in socket.io payload?

To get this working I had to change my on open message to

ws.Send("40[<data>]")

As the client connected, this was first hitting the io.on('connection') event on the server side which would then send back to the client who's OnMessage was being fired and then when sending back with ws.Send("42[]").

There's probably some reason why but this is literally all I changed and it now seems to be something I can work with.

orgg
  • 97
  • 1
  • 9