Socket.IO

Is it possible to listen for object instantiation in Node.js?

ε祈祈猫儿з 提交于 2019-12-29 10:02:40
问题 I am working on a browser-game in Node.Js and I have this script : game.js >> var config = require('./game_config.js'); var mysql = require('mysql'); var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var connexion = mysql.createConnection({ 'host': config.DB_HOST, 'user' : config.DB_USER, 'password' : config.DB_PASS, 'database' : config.DB_NAME }); var Player = require('./server/class.player.js'); io.on('connect', function(socket) {

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

Java implementation for client Socket.io compatible with version 1.0

我是研究僧i 提交于 2019-12-29 07:15:18
问题 It is almost 1 week that I am wasting time trying to find a WORKING library that implements the client side of Socket.IO compatible with v. 1.0 Mentioning just the most relevant results...so far I found: 1. https://github.com/nkzawa/socket.io-client.java which I compiled (not without difficulties) and tried to link to my project without success since it gives me an error on the Emitter class that I couldn't solve. 2. https://github.com/Gottox/socket.io-java-client that is compatible till

Java implementation for client Socket.io compatible with version 1.0

可紊 提交于 2019-12-29 07:15:05
问题 It is almost 1 week that I am wasting time trying to find a WORKING library that implements the client side of Socket.IO compatible with v. 1.0 Mentioning just the most relevant results...so far I found: 1. https://github.com/nkzawa/socket.io-client.java which I compiled (not without difficulties) and tried to link to my project without success since it gives me an error on the Emitter class that I couldn't solve. 2. https://github.com/Gottox/socket.io-java-client that is compatible till

How to use socket.io to communicate with another server when the actual page is being served by a localhost server?

走远了吗. 提交于 2019-12-29 06:16:59
问题 I'm serving my page through localhost (XAMPP, Apache), and on my friend's physical server I run a node.js server that is used for communication with the page (a game). This is the node.js server code: var io = require('socket.io').listen(1235); io.sockets.on('connection', function (socket) { socket.on("start", function (data) { console.log(data); }); }); It runs without any errors, but I don't know how to include the socket.io code into my webpage! How do I do that? 回答1: Include a script tag

How to use socket.io to communicate with another server when the actual page is being served by a localhost server?

偶尔善良 提交于 2019-12-29 06:16:24
问题 I'm serving my page through localhost (XAMPP, Apache), and on my friend's physical server I run a node.js server that is used for communication with the page (a game). This is the node.js server code: var io = require('socket.io').listen(1235); io.sockets.on('connection', function (socket) { socket.on("start", function (data) { console.log(data); }); }); It runs without any errors, but I don't know how to include the socket.io code into my webpage! How do I do that? 回答1: Include a script tag

WebSocket connection failed with nginx, nodejs and socket.io

浪尽此生 提交于 2019-12-29 05:21:08
问题 I try to setup nodejs with nginx. But when the client try to connect it fails with... WebSocket connection to 'ws://www.mydomain.com/socket.io/1/websocket/KUv5G...' failed: Error during WebSocket handshake: 'Connection' header value is not 'Upgrade': keep-alive socket.io.js:2371 So how to enable websocket comunication? my current nginx config upstream mynodejsapp { server 127.0.0.1:3000 max_fails=0 fail_timeout=10s weight=1; ip_hash; keepalive 512; } server { listen 80; listen [::]:80 default

How to reconnect to websocket after close connection

五迷三道 提交于 2019-12-29 04:27:24
问题 I construct my websocket connection with this code (e.g.): var socket = new WebSocket("ws://94.12.176.177:8080"); And I close the connection with this one: socket.close(); But how do I reestablish connection? I've done some research and tried several methods. This question could not help me: Socket.io reconnect on disconnect? It's the only result which close to what I'm looking for. The reason I want to do this is to allow users to stop sending data to the web temporary, and resending again

What should I be using? Socket.io rooms or Redis pub-sub?

会有一股神秘感。 提交于 2019-12-29 02:24:06
问题 Pretty simple question. I am building a realtime game using nodejs as my backend and I am wondering if there is any information available on which one is more reliable and which one is more efficient? I am heavily using both Redis and Socket.io throughout my code. So I want to know whether I should be utilizing Socket.io's Rooms or I would be better off using redis' pub-sub ? Update: Just realized there is a very important reason why you may want to use redis pub/sub over socket.io rooms.

Socket.io emit on Express Route

拜拜、爱过 提交于 2019-12-28 11:51:03
问题 I want to emit some data to the client when some API route gets called. I have to following code on server.js var app = express(); var http = require('http').Server(app); var io = require('socket.io')(http); io.on('connection', function(socket){ console.log('a user connected'); socket.emit('tx', 'msg'); socket.on('disconnect', function(){ console.log('user disconnected'); }); }); Now I have this route /test: var bsg = require('./routes/test'); And that file: var express = require('express');