0

I want to use the tcp net module in node.js, my clients will be browser and also not browser ( device ). when I tried to run in the browser the index.html, my browser keeps loading looks like it looping..I dont know what's wrong in my code. I tried use telnet it works fine, the problem is on the browser i cannot load properly the index.html

//app.js

var net = require('net');
var io = require('socket.io')(net);




var clients = [];


var server = net.createServer(function (socket) {
    console.log("New client connected");
    clients.push(socket);
});





server.listen(1337, 'localhost', false, function () {
    console.log('server bound');
});



io.on('connection',function(socket){
    socket.emit('news', { hello: 'world' });
});

here is my client code.

http://pastie.org/10115599

jemz
  • 4,987
  • 18
  • 62
  • 102

1 Answers1

0

Both the browser and socket.io require an http server, not just a TCP server. You can see the code examples in the socket.io documentation. The first server and client code example on that doc page shows you the basics you need.

In fact, the first step in socket.io connection is an http request that is then "upgraded" to the webSocket protocol. So, the server must be an http server. And socket.io hooks into an http server in order to receive incoming connections.

Here's a code example from the socket.io doc:

Server Code:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);

app.listen(80);

io.on('connection', function (socket) {
    // incoming socket.io connection established
});

function handler (req, res) {
    // process http requests for normal web page serving    
}

Client Code:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • so I need to create another server app for http just like in the example provided ? – jemz Apr 27 '15 at 05:31
  • @jemz - You don't need another server app, you just need to make your one server be an http server, not a plain tcp server. – jfriend00 Apr 27 '15 at 05:34
  • if I change my code to http just like in your post, can I still receive the data that is being send by the client(device) ? – jemz Apr 27 '15 at 05:37
  • @jemz - I have no idea. Does your client device use http? Or just its own protocol on TCP? if the client device does not use HTTP, then you would need a separate server on a different port for it. socket.io must use HTTP to initiate the connection and the socket.io documentation examples assume you're using it from a browser and that the browser web page is served by the same http server. – jfriend00 Apr 27 '15 at 05:39
  • my client device is not using http, it's TCP, – jemz Apr 27 '15 at 05:41
  • why this examle https://nodesource.com/blog/understanding-socketio; uses net module, as I know that net module is for TCP not HTTP – jemz Apr 27 '15 at 05:44
  • @jemz - I don't know why that doc shows that. I do know that a socket.io connection starts with an HTTP request so it MUST be talking to a server that speaks HTTP. – jfriend00 Apr 27 '15 at 05:55
  • how do i send message to my client(device) connected in socket? the message is come from the browser – jemz Apr 27 '15 at 06:04
  • @jemz - didn't I already answer that here: http://stackoverflow.com/questions/29874261/how-do-i-send-message-to-specific-client-in-node-js – jfriend00 Apr 27 '15 at 06:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76326/discussion-between-jemz-and-jfriend00). – jemz Apr 27 '15 at 06:31