socket.io-1.0

If node server is down. How to get error handling on socket.io

荒凉一梦 提交于 2019-12-01 12:30:01
问题 How can i detect server status ? var socket= io.connect('http://nodeserver.com:3000'); Here is my code. If nodeserver.com:3000 is down, how can i validate server down or up ? I tried connect_failed , error events but doesn't work. 回答1: have you tried doing it like this? this.socket.on('connect_error', function (err) { //do something }); in other words, use the connect_error instead of the connect_failed event? 回答2: for checking "disconect" of a socket you can you this code : io.socket.on(

How to do RPC over socket.io?

偶尔善良 提交于 2019-12-01 06:24:51
Suppose we have a simple echo server ( tuned to be longer on the first request ): var waiting = 8000; io.on('connection', function(socket){ socket.on('doEcho', function (data) { setTimeout( function () { socket.emit('echoDone', data); }, waiting); waiting = 1; }); }); Then say a index.html client script does: askEcho ("11111", function (resp) { console.log("answer for 11111 " + resp); }); askEcho ("22222", function (resp) { console.log("answer for 22222 " + resp); }); where askEcho is the following buggy function, pretending to be an RPC stub: function askEcho (text, fun) { socket.emit('doEcho

How to do RPC over socket.io?

梦想与她 提交于 2019-12-01 03:24:36
问题 Suppose we have a simple echo server ( tuned to be longer on the first request ): var waiting = 8000; io.on('connection', function(socket){ socket.on('doEcho', function (data) { setTimeout( function () { socket.emit('echoDone', data); }, waiting); waiting = 1; }); }); Then say a index.html client script does: askEcho ("11111", function (resp) { console.log("answer for 11111 " + resp); }); askEcho ("22222", function (resp) { console.log("answer for 22222 " + resp); }); where askEcho is the

Configure TimeOut and Transports in Socket.IO 1.0

时光总嘲笑我的痴心妄想 提交于 2019-11-30 23:47:12
In old versions of Socket.IO (< 0.9), I configured "close timeout" and "transports" this way: io.set("transports", ["xhr-polling"]); io.set("close timeout", 3); But now, the command io.set has been deprecated. How do I define close timeout and transports ? When I use the old version this message appears: Option close timeout is not valid. See the documentation here: http://socket.io/docs/migrating-from-0-9/#configuration-differences Basically, you just have to set those when you initialize the server: var socket = require('socket.io')({ // options go here }); 来源: https://stackoverflow.com

Get the list of rooms the client is currently in on disconnect event

杀马特。学长 韩版系。学妹 提交于 2019-11-30 20:09:50
I am trying to find the list of rooms a client is currently in on disconnect event (closes the browsers / reloading the page / internet connection was dropped). I need it for the following reason: user has entered few rooms. Then other people has done the same. Then he closes a browser tab. And I want to notify all the people in the rooms he is in that he left. So I need to do something inside of on 'disconnect' event. io.sockets.on('connection', function(client){ ... client.on('disconnect', function(){ }); }); I already tried two approaches and found that both of them are wrong: 1) iterating

Expressjs+socket.io+express-session

情到浓时终转凉″ 提交于 2019-11-30 19:38:40
问题 I have this code in my server.js. var app = require('express')(), session = require('express-session'), cookie = require('cookie'), cookieParser = require('cookie-parser'), manager = require('./sockets/manager'); var sessionStore = new session.MemoryStore(); app.use(cookieParser('secret')); app.use(session({ name: 'sid', store: sessionStore, secret: 'secret', saveUninitialized: true, resave: true, cookie: { path: '/', httpOnly: true, secure: false, maxAge: null } })); var server = require(

Configure TimeOut and Transports in Socket.IO 1.0

送分小仙女□ 提交于 2019-11-30 18:04:33
问题 In old versions of Socket.IO (< 0.9), I configured "close timeout" and "transports" this way: io.set("transports", ["xhr-polling"]); io.set("close timeout", 3); But now, the command io.set has been deprecated. How do I define close timeout and transports ? When I use the old version this message appears: Option close timeout is not valid. 回答1: See the documentation here: http://socket.io/docs/migrating-from-0-9/#configuration-differences Basically, you just have to set those when you

Get the list of rooms the client is currently in on disconnect event

孤者浪人 提交于 2019-11-30 03:30:53
问题 I am trying to find the list of rooms a client is currently in on disconnect event (closes the browsers / reloading the page / internet connection was dropped). I need it for the following reason: user has entered few rooms. Then other people has done the same. Then he closes a browser tab. And I want to notify all the people in the rooms he is in that he left. So I need to do something inside of on 'disconnect' event. io.sockets.on('connection', function(client){ ... client.on('disconnect',

io.sockets.socket(socket_id).emit() - has no method 'socket'

混江龙づ霸主 提交于 2019-11-29 07:32:41
im running a nodejs server + express + socket.io version 1.0.4 and everything works as expected in my app, but i just cant emit a message from server side to client side via: socket_server.sockets.socket(socket_id).emit("message", data); when i try server throws following error: TypeError: Object #<Namespace> has no method 'socket' but i can remember that code worked on my socket.io 0.7. /0.8. projects What am i doing wrong? :( a snippet out of my app.js (nodejs server): var express = require('express'); var app = express(); /* Server Start */ var server = app.listen(8080, function() { console

How to send binary data from a Node.js socket.io server to a browser client?

主宰稳场 提交于 2019-11-28 23:24:27
I've been looking through the entire Socket.IO docs, but, even though they promise it is there, I can't find a simple, minimal example, of how one would send binary data between server/client. How is it done? It is in fact in the documentation. The current documentation for Socket.io says under Socket.emit : [...] Emits an event to the socket identified by the string name. Any other parameters can be included. All datastructures are supported, including Buffer [...] So, if you can send a buffer, you can send binary data. All you have to do is to pack your data into a Buffer object. You may