publishService.js 951 B

1234567891011121314151617181920212223242526272829303132
  1. import { pubClient } from "../connection/redis.js";
  2. const prefix = process.env.REDIS_PREFIX || "chat";
  3. const getInKey = (realKey) => {
  4. return `${prefix}:${realKey}`;
  5. };
  6. const publishEnterRoom = (userId, roomId) => {
  7. const onlineConfigKey = `online:${roomId}:${userId}`;
  8. const payLoad = {
  9. userId,
  10. roomId,
  11. type: 0,
  12. createTime: Date.now(),
  13. };
  14. pubClient.hSet(getInKey(onlineConfigKey), `${payLoad.createTime}`, JSON.stringify(payLoad));
  15. pubClient.publish("enterRoom", JSON.stringify(payLoad));
  16. };
  17. const publishExitRoom = (userId, roomId) => {
  18. const onlineConfigKey = `online:${roomId}:${userId}`;
  19. const payLoad = {
  20. userId,
  21. roomId,
  22. type: 0,
  23. createTime: Date.now(),
  24. };
  25. pubClient.hSet(getInKey(onlineConfigKey), `${payLoad.createTime}`, JSON.stringify(payLoad));
  26. pubClient.publish("exitRoom", JSON.stringify(payLoad));
  27. };
  28. export { publishEnterRoom, publishExitRoom };