Tag: socket.io

监听节点中同一应用中的两个端口

我在端口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) […]

socket.io和node.js在localhost上给出错误

我有一个开发服务器在192.168.1.22上工作,我想运行node.js,但它会给出一些错误。 我只想testing一些情况,但是甚至不能运行它。 运行后,我将获取我的onclickbutton,并从node.js发送到我的PHP文件的post。 并从php返回数据到node.js和node.js到网站。 我的js: var http = require('http'), io = require('socket.io'), server = http.createServer(function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); res.write('<h1>Sample server created with NodeJS.</h1>'); res.end(); }); server.listen(8001); // socket.io var socket = io.listen(server); socket.on('connection', function(client){ client.send('Hello client'); client.on('message', function(){ client.send((new Date()).getTime()); }) }); 我的html: <script type="text/javascript" src="http://cdn.socket.io/stable/socket.io.js"></script> <script type="text/javascript"> var socket = new io.Socket(null, {port: […]

与UTF8数据错误

我正在尝试使用socket.io创build一个非常简单的聊天应用程序。 它运行良好,但utf8数据。 当我尝试发送一些包含utf8内容的消息时,出现错误。 Error: Invalid continuation byte at Error (<anonymous>) at readContinuationByte (F:\nodejs\node_modules\socket.io\node_modules\engin e.io\node_modules\engine.io-parser\node_modules\utf8\utf8.js:127:9) at decodeSymbol (F:\nodejs\node_modules\socket.io\node_modules\engine.io\nod e_modules\engine.io-parser\node_modules\utf8\utf8.js:167:12) at Object.utf8decode [as decode] (F:\nodejs\node_modules\socket.io\node_modu les\engine.io\node_modules\engine.io-parser\node_modules\utf8\utf8.js:201:17) at Object.exports.decodePacket (F:\nodejs\node_modules\socket.io\node_module s\engine.io\node_modules\engine.io-parser\lib\index.js:150:17) at WebSocket.Transport.onData (F:\nodejs\node_modules\socket.io\node_modules \engine.io\lib\transport.js:102:24) at WebSocket.onData (F:\nodejs\node_modules\socket.io\node_modules\engine.io \lib\transports\websocket.js:75:30) at WebSocket.EventEmitter.emit (events.js:98:17) at Receiver.self._receiver.ontext (F:\nodejs\node_modules\socket.io\node_mod ules\engine.io\node_modules\ws\lib\WebSocket.js:697:10) at Receiver.opcodes.1.finish (F:\nodejs\node_modules\socket.io\node_modules\ engine.io\node_modules\ws\lib\Receiver.js:397:14) Program node chat.js exited with code 8 我怎么解决它? […]

Node.js:SocketIO vs http请求性能

我有这种情况下,我需要将数据存储在数据库中,并发出一个事件只有特定的套接字。 这可以通过两种方式来完成: 从客户端发送一个事件,在服务器端收听这个事件,存储数据并把事件发送到我们想要的套接字。 发送一个http请求到服务器,在那里存储数据并把事件发送到套接字。 我想知道这些方法中哪一个是有效的,并且比这两个方法有更好的解决scheme。

用户收到来自nodejs的超过1个通知

这是我第一次使用nodejs,并且遇到了一些问题。 主要的问题是用户从服务器接收到超过1个信号。 计数基于页面的刷新。 以下是我的代码: var http = require('http'); var server = http.createServer().listen(1332); var io = require('socket.io').listen(server); var redis = require('redis'); var sub = redis.createClient(); //Subscribe to the Redis chat channel sub.subscribe('notification_count'); console.log("Server is running…\nClick on Ctrl+C to exit"); io.sockets.on('connection', function (socket) { var user_id = socket["handshake"]["query"]["user_id"]; console.log("user_id", user_id); socket.room = user_id; socket.join(user_id); socket.on('disconnect', function(){ socket.leave(socket.room); […]

Unity3d和Sockets.IO不会握手

我使用https://github.com/kaistseo/UnitySocketIO-WebSocketSharp来启用Unity3D与Node.JS Socket.IO服务器通信。 但是在握手过程中出错 套接字错误:初始化握手与服务器时出错 客户代码: SocketIOClient.Client socket; // Use this for initialization void Start () { socket = new SocketIOClient.Client("http://server:80/"); socket.On("connect", (fn) => { Debug.Log ("connect – socket"); Dictionary<string, string> args = new Dictionary<string, string>(); args.Add("msg", "what's up?"); socket.Emit("SEND", args); }); socket.On("RECV", (data) => { Debug.Log (data.Json.ToJsonString()); }); socket.Error += (sender, e) => { Debug.Log […]

socket.io v0.9和v1是否兼容?

socket.io v0.9和v1是否兼容? 运行v1的客户端可以连接到运行v0.9的服务器,反之亦然?

如何连接到现有的套接字io连接

我的应用程序这样工作: 用户login到网站 JavaScript连接到Socket.IO服务器,并传输PHPSESSID Socket.IO使用PHPSESSID连接到数据库,然后获取user_id 使用user_id我们再连接一次数据库来获取user_place 用户然后连接到房间user_place并可以在这里与其他人交谈 事情是, user_place可以通过,例如,另一个PHP脚本更改。 所以当它改变的时候, 我需要告诉当前套接字io连接从数据库中获取新的“user_place” ,所以它可以离开当前房间并连接到新的。 我怎样才能做到这一点? 我有两个猜测: 执行php脚本时,连接到现有的Socket.IO连接并执行如client.on('update_place'), function() {} 。 问题是我不知道如何连接到一个现有的连接 (是否有可能?) 有一个setTimeout()函数检查数据库是否“user_place”已更改或不(但perfs将是可怕的) 以下是节点服务器的一些简化代码: io.sockets.on('connection', function(client) { var user_place; // Read client cookies if(client.handshake.headers['cookie']) { var cookies = cookie.parse(client.handshake.headers['cookie']); } // If cookies exist, connect to database and get “user_place” if(cookies) { connection.query("SELECT session_value, session_time FROM sessions WHERE […]

解码jwt令牌 – 是否安全?

使用express,socket.io和基于令牌的authentication来build立testing服务器 看了这个教程https://auth0.com/blog/2014/01/15/auth-with-socket-io/ 在服务器端,教程logging解码的令牌 console.log(socket.handshake.decoded_token.email, 'connected'); 但是,当我尝试loginsocket.handshake.decoded_tokenvariables是未定义的socket.handshake不包含任何variables与解码令牌 所以..我试图谷歌如何解码令牌,发现这个页面https://developers.google.com/wallet/digital/docs/jwtdecoder 我粘贴公共令牌和脚本解码令牌没有jwtSecret!? 嗯…然后我在想..如果脚本可以解密令牌而没有秘密,怎么可能安全呢? 作为authentication返回给客户端的公共令牌 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImNsYXJrIiwiZW1haWwiOiJjbGFya0BlbGVjdHJvYmVhdC5kayIsImlhdCI6MTQwMzczMTkyMSwiZXhwIjoxNDAzNzM1NTIxfQ.mVFymk6gKBPmcVObB_3ydqbJTlcv4eVNYBcahsjg0g8

自定义logging器和Socket.io 1.x

在Socket.io的 1.0之前的版本中,似乎可以在实例化时使用logging器选项来设置logging器 ,但是我似乎无法在1.0之后做到这一点。 我是正确的,他们已经删除了这个选项,有没有什么好的方法,我可以通过钩住/侦听错误从Socket.io似乎现在使用debugging模块,而无需修改现有的模块。 我想用Bunyan来logging我的socket信息。