2011-12-15 161 views
3

這應該是相當微不足道的,但無論什麼原因,我有一個 連接ExtJS4到一個socket.io/node.js服務(使用最新版本npm和節點0.6.3)。我正在嘗試向我的服務器發送消息。它握手,但它永遠不會進入消息塊。我使用「emit」從socket.io-client發送它,但沒有運氣。任何想法我做錯了什麼?socket.io與ExtJS和socket.io客戶端

我有我所有的代碼,並從如下服務器調試出來(從尼爾斯Dehl優秀example分叉)...

我有這樣的代碼爲我的server.js:

var http = require('http'), 
      sys = require('util'), 
      fs = require('fs'), 
      io = require('socket.io'); 
var Connect = require('connect'); 

var server = Connect.createServer(
     Connect.logger(), // Log responses to the terminal using Common Log Format. 
     Connect.staticCache(), // Add a short-term ram-cache to improve performance. 
     Connect.profiler(), 
     Connect.favicon(), 
     Connect.static(__dirname + "/public") // Serve all static files in the current dir. 
); 
var sockets = io.listen(server); 
server.listen(4000); 

sockets.on('connection', function(socket) { 
console.log("Receiving msg..."); 
     var user; 

     socket.on('message', function(message) { 
       if (!user) { 
         user = message; 
         socket.send({ message: 'Welcome, ' + user.nickname + '!', nickname: 'server', gravatar: '' }); 
         return; 
       } 
       var response = { 
         'nickname': user.nickname, 
         'gravatar': user.gravatar, 
         'message': message.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;") 
       }; 
       sockets.emit(response); 
     }); 

}); 

然後在客戶端,我使用ExtJS和socket.io-client發送 消息。我在ExtJS的界面看起來像這樣調入 插座io.client:

App.util.Socketio = Ext.extend(Ext.util.Observable, { 
     constructor: function(options){ 
       options = options || {}; 
       App.util.Socketio.superclass.constructor.call( 
         this 
       ); 
       this.socket = new io.Socket(options); 
       var that = this; 
       this.socket.on('connect', function(){ 
         that.onConnect(); 
       }); 
       this.socket.on('message', function(data){ 
         that.onMessage(data); 
       }); 
       this.socket.on('close', function(){ 
         that.onClose(); 
       }); 
       this.socket.on('disconnect', function(){ 
         that.onDisconnect(); 
       }); 
     }, 
     connect: function() { 
       this.socket.connect(); 
     }, 
     disconnect: function(){ 
       this.socket.disconnect(); 
     }, 
     send: function(message) { 
       this.socket.emit(message); 
     }, 
     onConnect: function() { 
       this.fireEvent('connect'); 
     }, 
     onDisconnect: function() { 
       this.fireEvent('disconnect'); 
     }, 
     onClose: function() { 
       this.fireEvent('close'); 
     }, 
     onMessage: function(message) { 
       this.fireEvent('message', message); 
     } 
}); 

這是我看到的輸出:

info - socket.io started 
    debug - client authorized 
    info - handshake authorized 16300591002012036186 
    debug - client authorized 
    info - handshake authorized 4532571941614163017 
    debug - setting request GET /socket.io/1/websocket/ 
16300591002012036186 
    debug - set heartbeat interval for client 16300591002012036186 
    debug - client authorized for 
    debug - websocket writing 1:: 
    debug - setting request GET /socket.io/1/websocket/ 
4532571941614163017 
    debug - set heartbeat interval for client 4532571941614163017 
    debug - client authorized for 
    debug - websocket writing 1:: 
    debug - emitting heartbeat for client 16300591002012036186 
    debug - websocket writing 2:: 
    debug - set heartbeat timeout for client 16300591002012036186 
    debug - emitting heartbeat for client 4532571941614163017 
    debug - websocket writing 2:: 
    debug - set heartbeat timeout for client 4532571941614163017 
    debug - got heartbeat packet 
    debug - cleared heartbeat timeout for client 16300591002012036186 
    debug - set heartbeat interval for client 16300591002012036186 
    debug - fired heartbeat timeout for client 4532571941614163017 
    info - transport end 
    debug - set close timeout for client 4532571941614163017 
    debug - cleared close timeout for client 4532571941614163017 
    debug - discarding transport 

回答

1

兩件事情

  1. 代替socket.send({ message: 'Welcome, ' + user.nickname + '!', nickname: 'server', gravatar: '' })改爲socket.emit(hello, { message: 'Welcome, ' + user.nickname + '!', nickname: 'server', gravatar: '' }。您應該使用emit發送JSON

    示例 - https://github.com/parj/node-websocket-demo/blob/socket_emit/server.js

  2. socket.emit,你需要使用一個自定義事件 - sockets.emit('response', response);

在客戶端的

  1. 參考至 - socket.emit(hello, { message: 'Welcome, ' + user.nickname + '!', nickname: 'server', gravatar: '' }

    socket.on('hello', function (data) { 
        $('#message').text("Received - " + data.message); 
    }); 
    
  2. 參照 - sockets.emit('response', response);

    socket.on('resonse', function (data) { 
        $('#message').text("Received - " + data.response); 
    }); 
    

socket.send只是用於發送一個字符串 - 實施例socket.send("Hello");

有關socket.send一個完整工作示例見 - https://github.com/parj/node-websocket-demo 對於插座.emit see - https://github.com/parj/node-websocket-demo/tree/socket_emit/public