socket.mdc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ---
  2. description: 即时通讯(Socket)
  3. globs:
  4. ---
  5. # 即时通讯(Socket)
  6. `cool-admin`即时通讯功能基于[Socket.io(v4)](https://socket.io/docs/v4)开发,[midwayjs 官方 Socket.io 文档](http://midwayjs.org/docs/extensions/socketio)
  7. ## 配置
  8. `configuration.ts`
  9. ```ts
  10. import * as socketio from "@midwayjs/socketio";
  11. @Configuration({
  12. imports: [
  13. // socketio http://www.midwayjs.org/docs/extensions/socketio
  14. socketio,
  15. ],
  16. importConfigs: [join(__dirname, "./config")],
  17. })
  18. export class ContainerLifeCycle {
  19. @App()
  20. app: koa.Application;
  21. async onReady() {}
  22. }
  23. ```
  24. ## 配置`config/config.default.ts`
  25. 需要配置 redis 适配器,让进程之间能够进行通讯
  26. ```ts
  27. import { CoolConfig, MODETYPE } from "@cool-midway/core";
  28. import { MidwayConfig } from "@midwayjs/core";
  29. import * as fsStore from "@cool-midway/cache-manager-fs-hash";
  30. import { createAdapter } from "@socket.io/redis-adapter";
  31. // @ts-ignore
  32. import Redis from "ioredis";
  33. const redis = {
  34. host: "127.0.0.1",
  35. port: 6379,
  36. password: "",
  37. db: 0,
  38. };
  39. const pubClient = new Redis(redis);
  40. const subClient = pubClient.duplicate();
  41. export default {
  42. // ...
  43. // socketio
  44. socketIO: {
  45. upgrades: ["websocket"], // 可升级的协议
  46. adapter: createAdapter(pubClient, subClient),
  47. },
  48. } as MidwayConfig;
  49. ```
  50. ## 服务端
  51. ```ts
  52. import {
  53. WSController,
  54. OnWSConnection,
  55. Inject,
  56. OnWSMessage,
  57. } from "@midwayjs/core";
  58. import { Context } from "@midwayjs/socketio";
  59. /**
  60. * 测试
  61. */
  62. @WSController("/")
  63. export class HelloController {
  64. @Inject()
  65. ctx: Context;
  66. // 客户端连接
  67. @OnWSConnection()
  68. async onConnectionMethod() {
  69. console.log("on client connect", this.ctx.id);
  70. console.log("参数", this.ctx.handshake.query);
  71. this.ctx.emit("data", "连接成功");
  72. }
  73. // 消息事件
  74. @OnWSMessage("myEvent")
  75. async gotMessage(data) {
  76. console.log("on data got", this.ctx.id, data);
  77. }
  78. }
  79. ```
  80. ## 客户端
  81. ```ts
  82. const io = require("socket.io-client");
  83. const socket = io("http://127.0.0.1:8001", {
  84. auth: {
  85. token: "xxx",
  86. },
  87. });
  88. socket.on("data", (msg) => {
  89. console.log("服务端消息", msg);
  90. });
  91. ```
  92. ## 注意事项
  93. 如果部署为多线程的,为了让进程之间能够进行通讯,需要配置 redis 适配器,[配置方式](http://midwayjs.org/docs/extensions/socketio#%E9%85%8D%E7%BD%AE-redis-%E9%80%82%E9%85%8D%E5%99%A8)
  94. ```ts
  95. // src/config/config.default
  96. import { createRedisAdapter } from "@midwayjs/socketio";
  97. export default {
  98. // ...
  99. socketIO: {
  100. adapter: createRedisAdapter({ host: "127.0.0.1", port: 6379 }),
  101. },
  102. };
  103. ```