socket.io-1.0

Ajax vs Socket.io

我怕爱的太早我们不能终老 提交于 2019-12-17 02:23:23
问题 I am developing a web app and I was wondering which method should be suitable for my project. Basically what I want to display to the users are some notifications which are taken from requests to other servers. My node.js app gets all the info and then it spread out to the users, saving a copy into my MongoDB. The idea is quite simple but reading about methods I found these two techniques: Ajax : Client side would be checking all time if there is new content on the server. This would be done

Sending multiple emits inside a loop

送分小仙女□ 提交于 2019-12-13 07:07:36
问题 We currently need to send data to multiple connected client, each of them have a unique channel name, for instance CHANNEL1, CHANNEL2, CHANNEL3 Etc. When trying to cycle through all the channels for example, 30.000 channels. It sends the first one, then it waits till the loop finishes before sending the rest. io.on('connection', function (socket){ socket.on('chat message', function (msg){ for (i = 0; i < 30000; i++) { io.emit('CHANNEL' + i, msg); } }); }); Is there a way it sends the emits

socket.IO: how can i get the socketid when i emit to a specific client in socket.io 1.0

元气小坏坏 提交于 2019-12-13 02:26:52
问题 I want to emit a action to a specific client using socket.io 1.0. After reading Sending message to a specific ID in Socket.IO 1.0 , I know I can use: io.sockets.connected[socketid].emit() to emit a action to specific person. But how can I get the socketid ? I used to write like this : socket.id=nickName , but it's wont work. 回答1: Simply access id property of socket object: io.on('connection', function(socket) { console.log(socket.id); }); 来源: https://stackoverflow.com/questions/24135188

Socket.IO TypeError: Cannot read property 'emit' of undefined

為{幸葍}努か 提交于 2019-12-11 16:21:36
问题 I wrote the following code for chat client to client(client1 to server server to client2) but I am getting an error: Missing error handler on socket . TypeError: Cannot read property 'emit' of undefined Here is my serverside code. var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(3000); var users = {}; var reciverusers = {}; function handler (req, res) { fs.readFile(__dirname + '/index.html', function (err, data) { if (err)

How can I get the socket ID from an event in Socket.io?

霸气de小男生 提交于 2019-12-11 14:49:21
问题 I want to know which client sent an event when it arrives on the server. For example: var socket = require(socket.io')(port); socket.on("ask question", function(data){ var socketid = // ? //respond to sender socket.sockets.connected[socketid].emit("Here's the answer"); }); How can I get the socket ID of the event sender? 回答1: Your server-side logic is a bit off. The client connects and that's where the client socket is revealed and that's where you listen to events from a particular client.

How to emit error to be possible catch it on error handler on client-side?

梦想的初衷 提交于 2019-12-10 18:57:38
问题 I need to emit error in my connection handler and catch it on client-side something like this: // server-side.js var http = require('http'); var sio = require('socket.io'); var app = require('express')(); var server = require('http').createServer(app).listen(3000, function () { console.log('Server listening on 3000 port'); }); var io = sio.listen(server); io.on('connection', function (socket) { // do something and emit error socket.emit('error', new Error('Some error happend')); }); // cleant

socket.io doens't work with transports: [ 'xhr-polling' ]

牧云@^-^@ 提交于 2019-12-10 15:17:37
问题 I'm trying to test fallback to polling in socket.io, in order to verify that my app works with clients that don't support websockets for whatever reason. I am using the very basic server/client example for Express 4. It works fine with: // client-side var options = { transports: [ 'xhr-polling', 'websocket' ] }; var socket = io.connect('http://localhost:8080', options); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); However if I

Integrating node.js, socket.io and php web page

廉价感情. 提交于 2019-12-10 11:49:17
问题 I am going to integrate my php web page with node.js and socket.io. The client can connect to node.js server file but the socket.emit function does not work from client to the server or return. Here is my nodeClient.js file: var socket = io.connect( 'http://192.168.0.10:8080/index3.php' ); $( "#messageForm" ).submit( function() { var nameVal = $( "#nameInput" ).val(); var msg = $( "#messageInput" ).val(); socket.emit( 'message', { name: nameVal, message: msg }); // Ajax call for saving datas

Socket.IO: How do I remove a namespace

情到浓时终转凉″ 提交于 2019-12-10 04:05:04
问题 I need to be able to construct and destruct socket.io namespaces on-the-fly. It is easily to find information how to create a namespace, but I find nothing about how I remove/disconnect the namespace to release its memory. Say I got the following code already running: var nsp = io.of('/my-namespace'); nsp.on('connection', function(socket){ console.log('someone connected'): }); nsp.emit('hi', 'everyone!'); How do I disconnect/remove the socket.io namespace created above? 回答1: The io.of method

What do these numbers mean in socket.io payload?

徘徊边缘 提交于 2019-12-06 18:56:50
问题 When I am using native websocket API I can see just a payload in my chrome console for sockts: But when I use socket.io with their emit event, I can see some strange numbers before my actual payload. I do understand that colors mean that you either send or received the data, but what does the numbers like 42, 3, 2, 430, 420, 5 mean. Is there a place I can get a full list of these numbers with descriptions? The code which generates it is kind of big, so I just post small snippets. Client side