function broadcast (message) { if (_.isObject(message)) { // Array is also object. message = JSON.stringify(message) } connections.forEach(connection => { connection.sendUTF(message) }) }
function sendScore(){ const data = JSON.stringify({team0Score:scoreBlue, team1Score:scoreRed }); for(let i=0; i<maxclients; i++) { if(clients[i]) clients[i].connection.sendUTF(data); } }
function handleString(connection, message) { let data; try { data = JSON.parse(message.utf8Data); } catch(e) { connection.sendUTF(message.utf8Data); return; } handleJSON(connection, data, message); }
wsServer.on('request', function(request) { const connection = request.accept(null, request.origin); connection.on('message', function(message) { console.log('Received Message:', message.utf8Data); connection.sendUTF('Hi this is WebSocket server!'); }); connection.on('close', function(reasonCode, description) { console.log('Client has disconnected.'); }); });
_broadcastMessage(req, res, next) { for (let i = 0; i < this._connections.length; i++) { if (this._connections[i].state === 'open') this._connections[i].sendUTF(JSON.stringify(req.message)); } }
function move(object){ if(object.id < maxclients) objects[object.id].move(object.x, object.y); const data = JSON.stringify(object); for(let i=0; i<maxclients; i++) { if(clients[i] && (i !== object.id)) clients[i].connection.sendUTF(data); } }
setInterval(function () { var adc = mcp3008.readAdc(mcp3008.channels.CHANNEL_0); //console.log("Val:" + adc); var json = JSON.stringify({ type:'message', data: adc }); for (var i=0; i < clients.length; i++) { clients[i].sendUTF(json); } }, 500)
// Send data over the connection. // data - Buffer or string // send(data) { if (Buffer.isBuffer(data)) { data = data.toString(); } this.connection.sendUTF(data); }
_.each(connections, (connection) => { try { connection && connection.sendUTF('update') } catch (e) { debug(e) } })
send(message, cb) { if (cb == null) cb = null if (message.id == null) { message.id = this.next_message_id this.next_message_id++ } if (this.connected) this.conn.sendUTF(JSON.stringify(message)) else this.message_queue.push(message) if (cb) return this.waiting_cb[message.id] = cb }
setInterval(function() { for (var i=0; i < clients.length; i++) { console.log('server auto message'); clients[i].sendUTF('server auto message'); } },10000);
// create a listening socket net.createServer((sock) => { sock.on('data', (data) => { sock.end() sock.destroy() // send data to all connected clients for (let i = 0; i < connections.length; i++) { connections[i].sendUTF(data) } }) sock.on('close', (data) => { }) }).listen(SERVER_PORT)
// Called by the gyroscope (below) var broadcastGyroData = function(x, y, z) { var mess = { x: x, y: y, z: z }; for (var i=0; i < clients.length; i++) { clients[i].sendUTF(JSON.stringify(mess)); } }
let processData = (gps) => { let json = JSON.stringify({ type:'message', data: gps }); for (let i=0; i < clients.length; i++) { clients[i].sendUTF(json); } }