socket.io与ExtJS和socket.io-client

时间:2011-12-15 00:33:29

标签: extjs4 socket.io

这应该是微不足道的,但无论出于何种原因,我都有 将ExtJS4连接到socket.io/node.js服务(使用来自npm和节点0.6.3的最新版本)很麻烦。我正在尝试向我的服务器发送消息。它握手但它永远不会进入消息块。我正在使用“emit”从socket.io-client发送它,但没有运气。知道我做错了什么吗?

我有我的所有代码(从Nils 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中的界面看起来像这样调用 socket-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 

2 个答案:

答案 0 :(得分: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);

  3. 在客户端

    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);
       });
      
    3. socket.send仅用于发送字符串 - 示例socket.send("Hello");

      有关socket.send的完整工作示例,请参阅 - https://github.com/parj/node-websocket-demo 对于socket.emit,请参阅 - https://github.com/parj/node-websocket-demo/tree/socket_emit/public

答案 1 :(得分:0)

我为ExtJS做了一个WebSocket的扩展:ExtJS-WebSocket

在这里讨论论坛:Sencha Forum

可能很有趣;)