SLI
SLI

Reputation: 753

Nodejs Express + Socket.io + client on C#

i just don't understand why simple socket.io part don't work.

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');

var routes = require('./routes/index');

var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(3000, function(){
    console.log('listening on *:3000');
});

app.use(function(req, res, next) {
    console.log("INIT");
    console.log(req.headers['user-agent']);
    console.log(req.ip);
    next();
});

app.use('/', routes);

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
});

This is my cliend side code at C#. So when my nodejs server is online i don't get any errors from C#, so it's connecting, but i don't see it at node console. And this must work, i get this example here - https://www.youtube.com/watch?v=nwV3MS6pryY

using System;
using System.Net.Sockets;

namespace TCPSocketConsole
{
    class Program
    {
        static TcpClient mySocket = new TcpClient();
        static void Main(string[] args)
        {
            mySocket.Connect("127.0.0.1", 3000);
            Console.ReadLine();
        }
    }
}

So when i connect to http://localhost:3000 i don't get "a user connected" at my console. enter image description here

Upvotes: 3

Views: 4919

Answers (2)

jfriend00
jfriend00

Reputation: 708146

You are listening for a socket.io connection on your server, but your client is just make a plain TCP connection. The two protocols on each end must be the same. socket.io is not a plain TCP connection.

You can either listen for a plain TCP connection on your node.js server (and thus invent your own protocol) or you can get a class for a socket.io connection in your C# client so your C# client can actually speak to a socket.io server properly.

socket.io is based on webSocket and webSocket has a whole protocol for establishing the initial connection (it starts with an HTTP connection that is then upgraded to a webSocket connection) and then both webSocket and socket.io have their own framing for how data is sent. Both ends of the connection must speak the same protocol.

Upvotes: 4

catacs
catacs

Reputation: 311

In the socket.io docs you have an example of the client and server side. It seems your are not connecting from client side.

Upvotes: 2

Related Questions