這應該是相當微不足道的,但無論什麼原因,我有一個 連接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,"&").replace(/</g,"<").replace(/>/g,">")
};
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