这应该是相当微不足道的,但无论什么原因,我有一个 连接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