Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs & websockets

I'm trying to push data from my browser (Chrome) to a nodejs server and I'm having terrible trouble.

Basically, I've got this code that appears in the browser:

<script src="./Socket.IO/socket.io.js"></script>
<script>
        io.setPath('./Socket.IO/');

        var socket=new io.Socket('xxx.xxx.xxx.xxx');

        socket.on('connect',function(){
                alert('connect');       
        });
        socket.on('message',function(msg){
                alert('message'+msg);
        });
        socket.on('close',function(){
                alert('close');
        });
        socket.on('disconnect',function(){
                alert('disconnect');
        });
        socket.connect();

</script>

The only alert that's appearing is the 'close' alert.

Here is my server code:

var http=require('http');
var io=require('./Socket.IO-node');

var server=http.createServer(function(req, res){
    // your normal server code
    res.writeHeader(200, {'Content-Type': 'text/html'});
    res.writeBody('<h1>Hello world</h1>');
    res.finish();
});

var socket=io.listen(server,{transports:websocket,port:8080});

socket.on('connection',function(client){
        console.log('connection');
});

You can see I'm trying to log the connections to the console, but nothing ever shows up. I've been googling and trying to work through the Socket.IO examples at http://github.com/LearnBoost/Socket.IO-node and nothing seems to be working for me...

Any pointers is much appreciated.

Edit: Hi,

I've got the following server code now:

var http=require('http');
var io=require('./Socket.IO-node');

var server=http.createServer(function(req, res){
        //your normal server code
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('Hello world');
        res.end();
});

server.listen(8124);
server=io.listen(server);
server.on('connection', function(client){
        console.log('EOH connected');
        sys.log('EOH connected');
});
server.on('clientConnect',function(client){
        console.log('EOH connected');
        sys.log('EOH connected');
});
server.on('clientDisconnect',function(client){
        console.log('EOH disconnected');
        sys.log('EOH disconnected');
});

And the following client code:

<script>
window.onload=function(){
io.setPath('./Socket.IO/');
socket = new io.Socket('localhost', {'port': 8124});
socket.connect();
socket.send('xxx');
}
</script>

When I load the client code at localhost:8124, I'd expect some sort of 'clientConnect' events to be fired. I'm also sending data on the socket object and there's nothing appearing on the server... Totally stumped. Looking at using node-websocket-server now (http://github.com/miksago/node-websocket-server).

Solution: git clone git://github.com/LearnBoost/Socket.IO.git --recursive

Use --recursive flag. Doh!

like image 567
Eamorr Avatar asked Aug 02 '10 16:08

Eamorr


People also ask

What is NodeJS used for?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

Is NodeJS a programming language?

Node. js is not a programming language. Rather, it's a runtime environment that's used to run JavaScript outside the browser.

Is NodeJS frontend or backend?

Node. js is sometimes misunderstood by developers as a backend framework that is exclusively used to construct servers. This is not the case; Node. js can be used on the frontend as well as the backend.

Is NodeJS better than Java?

Java uses the concept of multithreading with ease, whereas Node JS does not use the concept of multi-threading like Java does. For large scale projects that involved concurrency, Java is highly recommended, whereas Node JS does not handle the thread and Java, which is the weakest point of this framework.


1 Answers

It's a Chrome issue: Node.js, WebSocket Location Issue?

like image 71
Ryan Avatar answered Sep 29 '22 12:09

Ryan