Browse Source

加入房间聊天mq

liuyu 5 years ago
parent
commit
da01d8cd84
1 changed files with 24 additions and 3 deletions
  1. 24 3
      app/service/roomchat.js

+ 24 - 3
app/service/roomchat.js

@@ -1,8 +1,9 @@
 'use strict';
 'use strict';
 
 
-// const assert = require('assert');
-// const _ = require('lodash');
-// const { ObjectId } = require('mongoose').Types;
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const moment = require('moment');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 // const { BusinessError, ErrorCode } = require('naf-core').Error;
 // const { BusinessError, ErrorCode } = require('naf-core').Error;
 
 
@@ -11,6 +12,26 @@ class RoomchatService extends CrudService {
     super(ctx, 'roomchat');
     super(ctx, 'roomchat');
     this.model = this.ctx.model.Roomchat;
     this.model = this.ctx.model.Roomchat;
   }
   }
+
+  async create(query, { number, sender_id, sender_name, content }) {
+    assert(sender_name, '缺少发言人信息');
+    assert(content, '缺少发言内容');
+    assert(number, '缺少房间号');
+    const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
+    const res = await this.model.create({ number, sender_id, sender_name, content, send_time });
+    // TODO MQ
+    const { mq } = this.ctx;
+    if (mq) {
+      const exchange = 'room_chat';
+      const parm = {
+        durable: true,
+        headers: {
+          number,
+        } };
+      await mq.fanout(exchange, number, JSON.stringify(res), parm);
+    }
+    return res;
+  }
 }
 }
 
 
 module.exports = RoomchatService;
 module.exports = RoomchatService;