使用node.js上的socket.on方法从浏览器获取数据

时间:2014-11-22 20:02:43

标签: javascript node.js sockets express

使用nodejs我将数据流式传输到客户端/浏览器并列出它们没有任何问题。我想要做的是通过点击它们将一些ID发送回服务器。无论如何,这是我的代码。我希望你能说出我在这种情况下做错了什么。

服务器端

var socketServer = io.listen(server);
socketServer.set('log level', 1); //don't log all emits etc

socketServer.sockets.on('connection', function(socket) {
    console.log('New connection logged');
    socketServer.sockets.emit('data', t.globalState);
});

//This is not working!
//I tried to put it in different places, no changes!
socketServer.sockets.on('hello', function(data) {
    console.log('Hello!');
}); 

socketServer.sockets.on('close', function(socket) {
    console.log('socketServer has closed');
});

var t = new conwit(config.conwit);

t.openStream = function () {
    console.log('Opening Stream');
    t.createStream();
};
t.createStream = function () {

    t.stream('statuses/filter', {
        track: t.globalState.tags,
        language: config.language
    }, function(stream) {

                stream.on('data', t.emitT);
                stream.on('error', function(error) {
                    console.log("My error: ", error);
                    setTimeout(function () {
                        t.openStream();
                    }, 30000);

                });

    });

};

t.emitT = function (data) {
        console.log(' receiving');
        if (data.text !== undefined) {
            socketServer.sockets.emit('data', data);
        }
};

t.openStream();

return t;

};

浏览器端

TM.socket.on('data', function(msg) {
   alert('News from server: ' + msg); //this works
   TM.socket.emit('hello', {msg:'Hello server'}); //this doesn't work!
});

$('#send').on('click', function(event) {
    event.preventDefault();
    TM.socket.emit('hello', { //this doesn't work
        rF: "foo",
        rT: "zazz",
        ea: "limk"
    });
});

双方都没有错误/日志消息。

1 个答案:

答案 0 :(得分:0)

将您的服务器代码更改为:

var socketServer = io.listen(server);
// Are you sure you have instantiated the config variable you use here?
var t = new conwit(config.conwit);
socketServer.set('log level', 1); //don't log all emits etc

// Every time a socket connects to the server, this callback executes.
// Notice that there is a convenient 'socket' variable to use as a 
// handle for this specific socket.
socketServer.sockets.on('connection', function(socket) {

    console.log('New connection logged');

    socketServer.sockets.emit('data', t.globalState);

    // This broadcasts the message to all the sockets (including the current 
    // one)
    socketServer.sockets.emit('data', 'Hey everybody! A new client has connected!')

    // This broadcasts the message to all the sockets but not the current
    // one
    socket.broadcast.emit('data', 'Hey everybody! A new client has connected!')

    // Now when a specific socket emits the 'hello' event, this callback will
    // execute...
    socket.on('hello', function(data) {
       console.log('Hello!');
       // ...and it will send a 'data' event back to the socket
       socket.emit('data', 'Hello client! This is your server speaking.');
    });

    // ...add more socket and/or t events here
});

请注意,有三种方法可以与套接字通信;

  1. socketServer.sockets.emit(event_name,event_data):这会向所有连接的套接字发出一个事件,包括触发事件的套接字。例如,假设网页上的红色按钮与此代码绑定:

    socket.emit('red button clicked');

    然后,在您的服务器上,您有以下代码:

    socket.on('red button clicked', function() { socketServer.sockets.emit('boom', 'A user clicked the red button'); });

    每当用户点击页面上的按钮时,所有客户(包括点击按钮的客户)都将收到活动'繁荣&#39>。

  2. socket.broadcast.emit(event_name,event_data):这与上面相同,唯一的区别是所有客户端都会收到该事件,但点击该事件的客户除外按钮。

  3. socket.emit(event_name,event_data):这只会发送'繁荣'向点击按钮的客户发送的事件。

  4. 有关更多信息,请参阅Socket.IO documentation