

socket.io使用示例–向指定客户端发消息
source link: https://blog.p2hp.com/archives/8444
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.

socket.io使用示例–向指定客户端发消息 | Lenix Blog
安装 node
创建一个目录例如socketio
在目录下执行
npm install socket.io
npm install redis
client: 代码如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>socket.io</title> <meta name="keywords" content=""> <meta name="description" content=""> </head> <body> <script src="node_modules/socket.io/client-dist/socket.io.js"></script> <script> const socket = io("ws://localhost:3000"); socket.on("connect", () => { console.log(socket.id); // 输出客户端的id }); // send a message to the server socket.emit("hello from client", 5, "6", { 7: Uint8Array.from([8]) }); // receive a message from the server socket.on("hello from server", (...args) => { console.log(args) }); </script> </body> </html>
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>socket.io</title>
- <meta name="keywords" content="">
- <meta name="description" content="">
- </head>
- <body>
- <script src="node_modules/socket.io/client-dist/socket.io.js"></script>
- <script>
- const socket = io("ws://localhost:3000");
- socket.on("connect", () => {
- console.log(socket.id); // 输出客户端的id
- // send a message to the server
- socket.emit("hello from client", 5, "6", { 7: Uint8Array.from([8]) });
- // receive a message from the server
- socket.on("hello from server", (...args) => {
- console.log(args)
- </script>
- </body>
- </html>
<!doctype html> <html> <head> <meta charset="utf-8"> <title>socket.io</title> <meta name="keywords" content=""> <meta name="description" content=""> </head> <body> <script src="node_modules/socket.io/client-dist/socket.io.js"></script> <script> const socket = io("ws://localhost:3000"); socket.on("connect", () => { console.log(socket.id); // 输出客户端的id }); // send a message to the server socket.emit("hello from client", 5, "6", { 7: Uint8Array.from([8]) }); // receive a message from the server socket.on("hello from server", (...args) => { console.log(args) }); </script> </body> </html>
Server.mjs 代码如下: 使用 redis 存储客户端socket.id,参考https://socket.io/docs/v4/rooms/
import { createServer } from "http"; import { Server } from "socket.io"; import redis from 'redis'; const httpServer = createServer(); const io = new Server(httpServer, { cors: { origin: "*" } }); httpServer.listen(3000); var client = redis.createClient()//用于存储客户端socket.id /* createClient({ url: 'redis://alice:[email protected]:6380' });*/ await client.connect(); client.on("error", function (err) { console.log("Error " + err); }); io.on("connection", (socket) => { // send a message to the client console.log(socket.id);//打印客户端socket的id socket.emit("hello from server", 1, "2", { 3: Buffer.from([4]) }); // receive a message from the client socket.on("hello from client", async (...args) => { // Save the socket id to Redis so that all processes can access it. await client.set("mastersocket", socket.id); const value = await client.get('mastersocket'); console.log('ssssssssssss',value); setInterval(function(){ io.to(value).emit("hello from server", 1, value, { 3: Buffer.from([4]) });//向指定客户端发送消息 }, 3000); console.log('message: ' + args); }); });
- import { createServer } from "http";
- import { Server } from "socket.io";
- import redis from 'redis';
- const httpServer = createServer();
- const io = new Server(httpServer, {
- cors: {
- origin: "*"
- httpServer.listen(3000);
- var client = redis.createClient()//用于存储客户端socket.id
- createClient({
- url: 'redis://alice:[email protected]:6380'
- });*/
- await client.connect();
- client.on("error", function (err) {
- console.log("Error " + err);
- io.on("connection", (socket) => {
- // send a message to the client
- console.log(socket.id);//打印客户端socket的id
- socket.emit("hello from server", 1, "2", { 3: Buffer.from([4]) });
- // receive a message from the client
- socket.on("hello from client", async (...args) => {
- // Save the socket id to Redis so that all processes can access it.
- await client.set("mastersocket", socket.id);
- const value = await client.get('mastersocket');
- console.log('ssssssssssss',value);
- setInterval(function(){
- io.to(value).emit("hello from server", 1, value, { 3: Buffer.from([4]) });//向指定客户端发送消息
- }, 3000);
- console.log('message: ' + args);
import { createServer } from "http"; import { Server } from "socket.io"; import redis from 'redis'; const httpServer = createServer(); const io = new Server(httpServer, { cors: { origin: "*" } }); httpServer.listen(3000); var client = redis.createClient()//用于存储客户端socket.id /* createClient({ url: 'redis://alice:[email protected]:6380' });*/ await client.connect(); client.on("error", function (err) { console.log("Error " + err); }); io.on("connection", (socket) => { // send a message to the client console.log(socket.id);//打印客户端socket的id socket.emit("hello from server", 1, "2", { 3: Buffer.from([4]) }); // receive a message from the client socket.on("hello from client", async (...args) => { // Save the socket id to Redis so that all processes can access it. await client.set("mastersocket", socket.id); const value = await client.get('mastersocket'); console.log('ssssssssssss',value); setInterval(function(){ io.to(value).emit("hello from server", 1, value, { 3: Buffer.from([4]) });//向指定客户端发送消息 }, 3000); console.log('message: ' + args); }); });
运行node Server.mjs测试
Recommend
-
63
cotopaxi是用于IoT设备安全测试的工具集。你可以指定IoT网络协议(如CoAP,DTLS,HTCPCP,mDNS,MQTT,SSDP)进行测试。 安装 只需从git克隆代码即可: https://github...
-
14
之前写的实现简单网络通信的代码,有一些严重bug。后面详细写。 根据上次的代码,主要增加了用户注册,登录页面,以及实现了实时显示当前在登录状态的人数。并解决一些上次未发现的bug。(主要功能代码参见之前随笔
-
12
redisgo包 redisgo是一款go语言的redis客户端库。 为了简化对redis的操作,可以使用redisgo对
-
10
V2EX › 程序员 求助,客户端访问数据库怎么指定端口号 shenxj · 22 小时 11 分钟前 · 616...
-
5
Python socket如何实现服务端和客户端数据传输(TCP)
-
9
最近在一个 react 项目中,使用到了 socket.io 处理即时消息,这里面有几点容易被忽视的问题,例如:在 React 单页面应用中如何防止出现多个 socket 实例、在任意的的组件内如何方便的取到 socket 实...
-
7
使用Java客户端发送消息和消费的应用
-
7
实全软件科技有限公司 衡斅-衡量使人觉悟
-
5
websocket服务端与客户端代码示例. ( json socket ) 用到了ws库. 步骤:1安装ws
-
7
Socket.D 基于消息的响应式应用层网络协议 首先...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK