在节点中的同一个应用程序中侦听两个端口

时间:2014-05-26 10:28:24

标签: node.js express redis socket.io

我在端口8081上使用socket.io并在8080上使用redis表示。

我不能在同一个端口上使用它们,否则我会发生冲突。当运行下面的代码时,控制台会记录php会话,我收到socket.io started消息,但是socket.io没有进一步记录到控制台。

编辑:

下面的代码已修改,现在我可以记录socket.io但我现在看不到cookie数据。

我不确定出了什么问题?

编辑 - 添加测试代码

完整的测试代码:

https://www.dropbox.com/sh/nbrmkeq7dwurizi/AADBvl4N5ksnCnFZ6MHFkIyva

服务器代码:

    var express = require('express'),
    http = require('http'),
    app = express(),
    server = http.createServer(app),
    io = require('socket.io').listen(8081),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    RedisStore = require('connect-redis')(session);

app.use(cookieParser());

app.use(session({
    store: new RedisStore({
        prefix: 'session:php:'
    }),
    name: 'PHPSESSID',
    secret: 'node.js rules'
}));

app.use('/', function(req, res) {
    console.log(req.session);
    res.sendfile('/');
});

io.sockets.on('connection', function (socket) {

    socket.on('subscribe', function(room) {
        console.log('joining room', room);
        socket.join(room);
    });
});

app.listen(8080);

相关客户端代码:

$(window).load(function () {

    var socket = io.connect('http://localhost:8081');

    var $conversation_id = $chat.data('conversation-id');

    socket.emit('subscribe', $conversation_id);
});

调用套接字IO JS文件:

<script type="text/javascript" src="http://me*****.dev:8081/socket.io/socket.io.js"></script>

控制台输出:

 info  - socket.io started
 debug - client authorized
 info  - handshake authorized w8S6Kc-XSFNq1N_pZErF
 debug - setting request GET /socket.io/1/websocket/w8S6Kc-XSFNq1N_pZErF
 debug - set heartbeat interval for client w8S6Kc-XSFNq1N_pZErF
 debug - client authorized for
 debug - websocket writing 1::
 info  - transport end (undefined)
 debug - set close timeout for client w8S6Kc-XSFNq1N_pZErF
 debug - cleared close timeout for client w8S6Kc-XSFNq1N_pZErF
 debug - cleared heartbeat interval for client w8S6Kc-XSFNq1N_pZErF
 debug - discarding transport
 debug - client authorized
 info  - handshake authorized hkMkIJXGH3ISmbzMZErG
 debug - setting request GET /socket.io/1/websocket/hkMkIJXGH3ISmbzMZErG
 debug - set heartbeat interval for client hkMkIJXGH3ISmbzMZErG
 debug - client authorized for
 debug - websocket writing 1::
 info  - transport end (undefined)
 debug - set close timeout for client hkMkIJXGH3ISmbzMZErG
 debug - cleared close timeout for client hkMkIJXGH3ISmbzMZErG
 debug - cleared heartbeat interval for client hkMkIJXGH3ISmbzMZErG
 debug - discarding transport
 debug - served static content /socket.io.js
 debug - client authorized
 info  - handshake authorized -L7kx-SdPrraKV3DZErH
 debug - setting request GET /socket.io/1/websocket/-L7kx-SdPrraKV3DZErH
 debug - set heartbeat interval for client -L7kx-SdPrraKV3DZErH
 debug - client authorized for
 debug - websocket writing 1::
 joining room 1

1 个答案:

答案 0 :(得分:1)

您不会在路线中向客户发送回复。在使用socket.io

之前创建有效的html页面
app.get('/', function(req, res) {
   res.end('<html><body><script type="text/javascript"></script></body></html>');
});

使用您的相关客户端代码

向客户发送有效回复