node+uni-app实现微信聊天功能

1、准备工作

https://socket.io/get-started/chat/

http://weappsocket.matong.io/

不一定要使用uni-app,也可以使用其它

2、node核心代码

const express = require("express");
const app = express();
let ioListen = app.listen("8082");
let io = require("socket.io").listen(ioListen);
let count = 0;
io.on('connection', (socket) => {
    socket.on("ag-message",data=>{
        socket.broadcast.emit("ag-gbmsg",data);//广播出去
    });
    socket.on("welcome",data=>{
        count = count + 1;
        socket.broadcast.emit("ag-welcome",{//自己不可以见
            info:data.info,
            count:count
        });
        socket.emit("ag-welcome",{//自己可见
            info:data.info,
            count:count
        });
    });
    socket.on("deleteCount",data=>{
        count = count - 1; 
        socket.broadcast.emit("updateCount",data,count);
    });
});

app.listen("6070",()=>{
    console.log("http://localhost:6070");
});

module.exports = app;

3、uni-app核心代码

3、main.js

import Vue from 'vue'
import App from './App'
let io = require("@/common/weapp.socket.io.js");
let socket = io('http://localhost:8082');
Vue.config.productionTip = false
Vue.prototype.socket = socket;

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

 

你可能感兴趣的:(node)