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.'); }); });
function createServer (httpServer) { const wsServer = new WebSocketServer({ httpServer, autoAcceptConnections: false }) wsServer.on('request', function(request) { const connection = request.accept('echo-protocol', request.origin); connections.push(connection) console.log((new Date()) + ' Connection accepted.'); connection.on('close', function(reasonCode, description) { connections.splice(connections.indexOf(connection), 1) console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.') }) }) console.log('WebSocket server injected.') }
/** * @param {http.Server} httpServer - Node HTTP/HTTPS compatible server. * @param {Object} [options] - Options for WebSocket-Node.WebSocketServer. * * @emits {info: Object, accept: Function, reject: Function} connectionrequest */ constructor(httpServer, options) { super(logger); logger.debug('constructor() [option:%o]', options); // Merge some settings into the given options. options = Object.assign( { httpServer : httpServer, keepalive : true, keepaliveInterval : 60000 }, options); // Run a WebSocket server instance. // @type {WebSocket-Node.WebSocketServer} this._wsServer = new websocket.server(options); this._wsServer.on('request', (request) => this._onRequest(request)); }
// This callback function is called every time someone // tries to connect to the WebSocket server wsServer.on('request', function(request) { console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); var connection = request.accept(null, request.origin); clients.push(connection); console.log((new Date()) + ' Connection accepted.'); // user sent some message connection.on('message', function(message) { if (message.type === 'utf8') { // accept only text console.log((new Date()) + ' Received Message: ' + message.utf8Data); var obj = { time: (new Date()).getTime(), text: message.utf8Data }; // broadcast message to all connected clients. That's what this app is doing. var json = JSON.stringify({ type:'message', data: obj }); for (var i=0; i < clients.length; i++) { clients[i].sendUTF(json); } } }); // user disconnected connection.on('close', function(connection) { // Close }); });
wsServer.on('request', function(request) { var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted.'); if(firstPacket.length){ /** * Every user will get beginnig of stream **/ firstPacket.map(function(packet, index){ connection.sendBytes(packet); }); } /** * Add this user to collection */ wsClients.push(connection); connection.on('close', function(reasonCode, description) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });
ws.on ('request', (request) => { tm.on("cmd", async function(cmd) room.on("participants",(updateParticipants = (participants) => { tm.event("participants", participants); })); }); participant.on("renegotiationneeded",async (sdp) => { participant.on("closed",function(){ connection.on("close", function(){ console.log("connection:onclose");
// This callback function is called every time someone // tries to connect to the WebSocket server wsServer.on('request', function(request) { console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); // accept connection - you should check 'request.origin' to // make sure that client is connecting from your website // (http://en.wikipedia.org/wiki/Same_origin_policy) var connection = request.accept(null, request.origin); // we need to know client index to remove them on 'close' event for (var i = 0; i < 5; i++) { connection.sendUTF( JSON.stringify({ type:'ADD_MESSAGE', payload: 'hello' })); } // user sent some message connection.on('message', function(message) { console.log('message'); connection.sendUTF( JSON.stringify({ type:'ADD_MESSAGE', payload: 'hello' })); }); // user disconnected connection.on('close', function(connection) { }); });
wsServer.on('request', function(request) { var connection = request.accept(null, request.origin); const id = clientId; clientId+=1; if(clientId >=maxclients) clientId = 0; connection.on('message', function(message) { let json; if (message.type === 'utf8') { connection.on('close', function(connection) { console.log("bye "+id); clients[id] = null;
wsServer.on('request', function(request) { console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); connection.on('message', function(message) { if (message.type === 'utf8') { // accept only text connection.on('close', function(connection) { if (userName !== false && userColor !== false) { console.log((new Date()) + " Peer "
// This callback function is called every time someone // tries to connect to the WebSocket server wsServer.on('request', (request) => { console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); let connection = request.accept(null, request.origin); clients.push(connection); console.log((new Date()) + ' Connection accepted.'); // user sent some message connection.on('message', (message) => { if (message.type === 'utf8') { // accept only text console.log((new Date()) + ' Received Message: ' + message.utf8Data); let obj = { time: (new Date()).getTime(), text: message.utf8Data }; // broadcast message to all connected clients. That's what this app is doing. let json = JSON.stringify({ type:'message', data: obj }); for (let i=0; i < clients.length; i++) { clients[i].sendUTF(json); } } }); // user disconnected connection.on('close', (connection) => { // Close }); });