Socket.IO

Getting “socket hang up” using nodejs / socket.io and wscat

我们两清 提交于 2020-12-29 12:01:30
问题 I have the following nodejs web socket server var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(8000); app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); console.log("Listening on 8000"); When trying to connect using wscat wscat -c ws:/

Getting “socket hang up” using nodejs / socket.io and wscat

自闭症网瘾萝莉.ら 提交于 2020-12-29 12:00:56
问题 I have the following nodejs web socket server var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(8000); app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); console.log("Listening on 8000"); When trying to connect using wscat wscat -c ws:/

Getting “socket hang up” using nodejs / socket.io and wscat

风格不统一 提交于 2020-12-29 12:00:21
问题 I have the following nodejs web socket server var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(8000); app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); console.log("Listening on 8000"); When trying to connect using wscat wscat -c ws:/

nodejs系列(10)实现socket通信

五迷三道 提交于 2020-12-29 11:24:19
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zeping891103/article/details/79287969 Socket主要作用是实现客户端与服务端的实时通信保持通话,它不像ajax请求,每次对话完成后都会把连接断开。Socket通信在Node.js中实现其实很简单,没有想象中复杂,基本上只要懂得监听(.on)和推送(.emit)消息,即能实现Socket通信。 Socket服务端 在服务端使用Socket,需先引入socket.io模块,该模块详细文档可参考https://socket.io/: cnpm install socket.io 服务端实例代码如下: var server = app.listen(8081, "127.0.0.1", function() { var host = server.address().address; var port = server.address().port; }); /********************socketIO********************/ var io = require('socket.io').listen(server); // 建立连接 io.sockets.on('connection', function(socket)

Node.js socket 双向通信

只愿长相守 提交于 2020-12-29 10:56:58
使用场景: 聊天室;大量数据常驻交互; 技术栈: Node.js, Vue.js || 原生JS 服务端代码: const app = require('http' ).createServer() const io = require('socket.io' )(app) app.listen( 8877 ) io.on( 'connection', scoket => { let i = 1 const t = setInterval(()=> { i ++ if (i >= 12 ) { clearInterval(t) } // 服务端往客户端发送消息 scoket.emit('news', { hello: 'world', t: new Date().getTime() }) }, 1000 ) // 服务端监听客户端的消息 scoket.on('receiveEvent', data => { console.log( 'receiveEvent: ' , data) }) }) 客户端代码: -- Vue 例子: <template> <div> <p @click="clientToServer">scoket:</p> <p v- for ="(item,index) in arr" :key="index">{{item}}</p> </div> <

How to store client associated data in socket.io 1.0

狂风中的少年 提交于 2020-12-29 02:53:45
问题 The docs say socket.io doesn't support .get .set now Is it okay to store client associated data like io.sockets.on('connection', function (client) { client.on('data', function (somedata) { client['data'] = somedata; }); }); in case I need multiple nodes? 回答1: Yes, it is OK to add properties to the socket.io socket object. You should be careful to not use names that could conflict with built-in properties or methods (I'd suggest adding a leading underscore or namescoping them with some sort of

How to store client associated data in socket.io 1.0

99封情书 提交于 2020-12-29 02:53:06
问题 The docs say socket.io doesn't support .get .set now Is it okay to store client associated data like io.sockets.on('connection', function (client) { client.on('data', function (somedata) { client['data'] = somedata; }); }); in case I need multiple nodes? 回答1: Yes, it is OK to add properties to the socket.io socket object. You should be careful to not use names that could conflict with built-in properties or methods (I'd suggest adding a leading underscore or namescoping them with some sort of

How to store client associated data in socket.io 1.0

隐身守侯 提交于 2020-12-29 02:50:39
问题 The docs say socket.io doesn't support .get .set now Is it okay to store client associated data like io.sockets.on('connection', function (client) { client.on('data', function (somedata) { client['data'] = somedata; }); }); in case I need multiple nodes? 回答1: Yes, it is OK to add properties to the socket.io socket object. You should be careful to not use names that could conflict with built-in properties or methods (I'd suggest adding a leading underscore or namescoping them with some sort of

Custom Events in Node.js with Express framework

老子叫甜甜 提交于 2020-12-28 07:43:31
问题 So, I'd like to know how to create custom events in node.js, and I'm hitting a wall. I'm pretty sure I'm misunderstanding something about how express works and how node.js events work. https://creativespace.nodejitsu.com That's the app. When a user creates a new "activity" (something that will happen many times) they send a POST request. Then within my route, if that POST succeeds I'd like to emit an event, that tells socket.io to create a new namespace for that activity. In my route file:

Custom Events in Node.js with Express framework

时光毁灭记忆、已成空白 提交于 2020-12-28 07:43:18
问题 So, I'd like to know how to create custom events in node.js, and I'm hitting a wall. I'm pretty sure I'm misunderstanding something about how express works and how node.js events work. https://creativespace.nodejitsu.com That's the app. When a user creates a new "activity" (something that will happen many times) they send a POST request. Then within my route, if that POST succeeds I'd like to emit an event, that tells socket.io to create a new namespace for that activity. In my route file: