socket.io-1.0

what does var io = require('../..')(server) do?

流过昼夜 提交于 2020-01-02 06:32:10
问题 I've build the project https://github.com/Automattic/socket.io/tree/master/examples/chat locally and it is working great. However, it would be nice to understand a little more about how a socket application works. In the main startup script one of the modules that is pulled in with require is var io = require('../..')(server) what does require('../..') do? thanks! 回答1: When a path to a directory is given to require , it will implicitly look for an index.js in that directory. In this case, it

Socket.IO 1.0.x: Get socket by id

大憨熊 提交于 2019-12-29 08:27:25
问题 In the 0.9.x version, we can get socket by ID like this: io.sockets.socket(socketId) But in 1.0.x we can't. How to find a socket by id in 1.0.x? 回答1: For socket.io 1.0 use: io.sockets.connected[socketId] For 0.9 its io.sockets.sockets[socketId] and not io.sockets.socket[socketId] 回答2: you can also use like: io.to(socketid).emit(); 回答3: Socket.io Version 2.0.3+ let namespace = null; let ns = _io.of(namespace || "/"); let socket = ns.connected[socketId] // assuming you have id of the socket 回答4

How to get room's clients list in socket.io 1.0

北城余情 提交于 2019-12-28 02:03:52
问题 I can get room's clients list with this code in socket.io 0.9. io.sockets.clients(roomName) How can I do this in socket.io 1.0? 回答1: Consider this rather more complete answer linked in a comment above on the question: https://stackoverflow.com/a/24425207/1449799 The clients in a room can be found at io.nsps[yourNamespace].adapter.rooms[roomName] This is an associative array with keys that are socket ids. In our case, we wanted to know the number of clients in a room, so we did Object.keys(io

Why is the broadcast described as flag in the docs when it is actually an object?

自作多情 提交于 2019-12-25 16:24:52
问题 In the broadcasting messages section http://socket.io/docs/#broadcasting-messages there is the following description To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it. Server var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.broadcast.emit('user connected'); }); The broadcast is an object. Why does the doc description refers it as

Determine URL of client request from server side. Socket.io

半世苍凉 提交于 2019-12-23 04:34:05
问题 Ref: Socket.io Client Request Origin URL Ref: Socket.io - How to get client URL request on server side? How do you determine the URL of client request from server side? Client request may come from multiple domains. 回答1: socket.io stores the request object from the original request that initiated the socket.io connection in socket.request . On this request object is: request.url request.headers The .url property will be the path of the URL (everything after the protocol, hostname and port).

latency issue with socket.io(websockets)

风流意气都作罢 提交于 2019-12-23 03:12:47
问题 we are planning to create a real time SPA using node.js and we are testing for the latency, please go through the following link : http://173.200.239.98:6060/ there are 2 area...when user hover the mouse over a textarea in right side, we are printing the latency in the left side textarea and the latency is in milliseconds. the ques is that latency varies from 0.3 secs to 6 secs..is it normal with web sockets? or am i doing something wrong? NOTE:- the server is in detroit,usa and i am

socket io, node js, Simple example to send image/files from server to client

喜欢而已 提交于 2019-12-18 09:59:44
问题 Is there any plain and straight forward examples on how to serve an image? from server to client? through buffering or just a direct call to download? (the goal is to get image files in near real time efficiently to sort of present a near live stream of images) and append to a html image tag or just in the body of the html page. incomplete sample code: (mostly acquired from official sample or just codes from stackoverflow) index.js // basic variables var app = require('express')(); var http =

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

对着背影说爱祢 提交于 2019-12-17 23:18:17
问题 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? 回答1: 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

Socket.io connection reverts to polling, never fires the 'connection' handler

℡╲_俬逩灬. 提交于 2019-12-17 22:58:41
问题 I'm trying to add socket.io to my existing node.js app on express. I've added the socket.io library in the server-side as follows (directly following http://socket.io/get-started/chat/): var express = require('express') , http = require('http') , path = require('path') , fs = require('fs'); var app = express(); var http = http.Server(app); var io = require('socket.io')(http); // Express settings [...] // Express routes [...] // Socket.io Communication io.on('connection', function(socket) {

Sending message to a specific ID in Socket.IO 1.0

▼魔方 西西 提交于 2019-12-17 10:08:48
问题 I want to sent data to one specific socket ID. We used to be able to do this in the older versions: io.sockets.socket(socketid).emit('message', 'for your eyes only'); How would I go about doing something similar in Socket.IO 1.0? 回答1: In socket.io 1.0 they provide a better way for this. Each socket automatically joins a default room by self id. Check documents: http://socket.io/docs/rooms-and-namespaces/#default-room So you can emit to a socket by id with following code: io.to(socketid).emit(