socketio命名空间和房间设置
source link: https://blog.p2hp.com/archives/9194
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1.命名空间
分隔来最大程度地减少资源(TCP连接)的数量
客户端
var qq = io.connect('http://localhost:3000/qq');
qq.on('connect', function() {
console.log("连接成功")
})
qq.on('news', function(data) {
console.log(data)
})
var wx = io('http://localhost:3000/wx');
wx.on('connect', function() {
console.log("连接成功")
})
wx.on('news', function(data) {
console.log(data)
})
服务器
io.of( “/路径” )
socketio.io = require('socket.io')(server);
var io = socketio.io;
var qq = io.of("/qq");
qq.on('connection', function(socket) {
qq.emit('news', {hello: 'qq'})
})
var wx = io.of("/wx");
wx.on('connection', function(socket) {
wx.emit('news', {hello: 'wx'})
})
2.加入房间群聊
可以join加入房间和leave离开房间。
客户端
var con = io.connect('http://localhost:3000');
con.on('connect', function() {
console.log('连接成功')
})
con.emit('joinRoom',{
room: 'room',
content: '进入房间'
})
con.on('roomEvent', function(data) {
console.log(data)
})
function send() {
con.emit('sendMsg', {
room: 'room',
content: '群发消息'
})
}
服务器socket.join(data.room); socket.to(data.room).emit(‘roomEvent’, data)
io.on('connection', function(socket) {
socket.on('joinRoom', function(data) {
console.log(data)
socket.join(data.room);
})
socket.on('sendMsg', function(data) {
socket.to(data.room).emit('roomEvent', data)
})
})
3.一对一发消息, 根据id发消息
var con = io('http://localhost:3000');
con.on('addUser', function(data) {
console.log(data);//获取广播的信息
})
function send(toId) {
con.emit('sendMsg', { fromid: con.id, toid: toId, msg: '客户端1'});
}
con.on('revMsg', function(data) {
console.log(data);
document.querySelector("h2").innerHTML = JSON.stringify(data);
});
io.on('connection', function(socket) {
io.sockets.emit('addUser', {
id: socket.id,
content: '广播有新用户加入'
})
socket.on('sendMsg', function(data) { //监听
console.log(data);
socket.to(data.toid).emit('revMsg', data)//发送对应id的客户端
})
})
Recommend
-
76
socket.io - Realtime application framework (Node.JS server)
-
94
README.md
-
47
引入依赖 新建一个spring-boot项目,不多说,可以用IDEA或者在 start.spring.io 上创建 spring-boot 项目 netty-socketio 可以去github上找最新的版本
-
53
README.md Gather-Tensorflow-Serving Gather how to deploy tensorflow models as much I can Covered Object Detection using Flask Sock...
-
21
One of the useful features of my Flask-SocketIO extension is the test client, which allows you to write Socket.IO unit tests. A long time limitation of the test client was that it did not see cookies set by Flask, such a...
-
24
Building a multi-room video chat with horizontal scaling ...
-
6
Tornado整合socketio(一)最近在做的项目中,需要将手机中的视频流或者音频流发送给服务端,再由服务端转发给浏览器端,起初我使用redis作为中转,将数据发到redis中,再由redis的发布订阅功能,整体架构如下 image-20210519225715299...
-
4
flask-socketio中文文档 2015-12-29 官网在这里,英语好的直接去看官网文档吧,我是英语渣只能翻译个大概;另外注意本文翻译时间,可能你看到...
-
6
新浪VR > 正文页 Quest v50 系统更新将引入多房间设置功能和更多追踪键盘...
-
2
(4)500代码行代码手写docker-设置网络命名空间 本系列教程主要是为了弄清楚容器化的原理,纸上得来终觉浅,绝知此事要躬行,理论始终不及动手实践来的深刻,所以这个系列会用go语言实现一个类...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK