监听节点中同一应用中的两个端口
我在端口8081上使用socket.io,并在8080上使用redis表示。
我不能在同一个端口上使用这两个端口,否则我会发生冲突。 当下面的代码运行时,php会话logging在控制台中,我得到了socket.io started
消息,但没有什么比从socket.iologin到控制台更远。
编辑:
下面的代码被修改,现在我可以loginsocket.io,但我现在看不到cookie数据。
我不确定什么是错的?
编辑 – 添加testing代码
完整的testing代码在这里:
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之前创build有效的html页面
app.get('/', function(req, res) { res.end('<html><body><script type="text/javascript"></script></body></html>'); });
并将您的相关客户端代码发送给客户