簡體   English   中英

偵聽節點中同一應用程序中的兩個端口

[英]Listening on two ports in the same app in node

我在端口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

您不會在/中發送響應給客戶端。 使用socket.io之前創建有效的html頁面

app.get('/', function(req, res) {
   res.end('<html><body><script type="text/javascript"></script></body></html>');
});

並使用您的相關客戶端代碼向客戶發送有效回復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM