@@ -51,11 +51,14 @@ export default {
5151 },
5252 ... mapGetters ([
5353 ' sidebar' ,
54- ' fullScreen'
54+ ' fullScreen' ,
55+ ' userInfo'
5556 ])
5657 },
5758 mounted () {
5859 this .initMqtt ()
60+ // 添加全局的键盘事件
61+ document .body .addEventListener (' keyup' , this .handleFullScreen )
5962 },
6063 methods: {
6164 // 全屏显示
@@ -79,23 +82,29 @@ export default {
7982 }
8083 },
8184 initMqtt () {
82- const URL = ' 127.0.0.1'
85+ const URL = process . env . VUE_APP_TYPE === ' localhost ' ? ' 127.0.0.1' : ' www.lyh.red '
8386 const client = mqtt .connect (` mqtt://${ URL } :1212` )
87+ const TopicList = [
88+ ` /chat/user/${ this .userInfo .id } ` ,
89+ ` /chat/group/#` ,
90+ ` /message/user/${ this .userInfo .id } ` ,
91+ ` /message/audit/#`
92+ ]
8493 // 连接
8594 client .on (' connect' , () => {
8695 console .log (' 连接' + new Date ())
87- const topic = ' /11123 '
88- client .subscribe (topic, function (err ) {
89- if (! err) {
90- client . publish (topic, ' Hello mqtt ' )
91- }
92- console . log ( ' 订阅成功: ' + topic )
96+ TopicList . forEach ( topic => {
97+ client .subscribe (topic, function (err ) {
98+ if (! err) {
99+ console . log ( ' 订阅成功: ' + topic )
100+ }
101+ } )
93102 })
94103 })
95104 // 获取到消息
96105 client .on (' message' , (topic , message ) => {
97106 // message is Buffer
98- console .log (message .toString ())
107+ console .log (topic, message .toString ())
99108 })
100109 // 断开自动重连
101110 client .on (' close' , () => {
0 commit comments