2014-11-22 78 views
0

使用nodejs我將數據流式傳輸到客戶端/瀏覽器,並且沒有任何問題地列出它們。我想要做的是通過點擊它們將一些ID發送回服務器。無論如何,這裏是我的代碼。我希望你能告訴我在這種情況下我做錯了什麼。使用node.js上的socket.on方法從瀏覽器獲取數據

服務器端

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" 
    }); 
}); 

沒有錯誤/兩側日誌消息。

回答

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'); });

    用戶每次點擊在頁面上的按鈕上,所有客戶端(包括點擊按鈕的客戶端)都將收到「繁榮」事件。

  2. socket.broadcast.emit(EVENT_NAME,EVENT_DATA):這是與上面,所有的客戶端將收到的情況下,除了誰上的按鈕點擊一個唯一的區別。

  3. socket.emit(event_name,event_data):這隻會將'boom'事件發送給點擊按鈕的客戶端。

欲瞭解更多信息,請參閱Socket.IO documentation

+0

這就像一個魅力。我錯過了socketServer.sockets.on中的** socket **變量('connection',function(** socket **)),並且yes ** config.conwit **正確傳遞。 – hayzem 2014-11-23 10:41:06

相關問題