uniapp 使用 socket.io通讯 服务端nodejs+express

uniapp

安装: npm i socket.io-client

    onLoad:function(){
	  var _this =this;
	  _this.socket = io.connect('http://localhost:30000/', {transports: ['websocket']});
	  _this.socket.on('connect', (res) => {
	  	 _this.socket.emit('StartConnection',"11212");
	  })
	  console.log(29,_this.socket);
	  _this.socket.on('enter', function (data) {
	        console.log(30,data)
	  })
    },

 

node 服务端代码

const socketIO = require("socket.io");
const io = socketIO(server);
const port = 30000;
let clientCount = 0;
io.on("connection", (socket) => {
  socket.on("StartConnection", async (device) => {
    clientCount++;
    socket.emit("enter",device);
    socket.on('message', function (str) {
      console.log("123213213")
    })
 })
})
server.listen(port, function () {
  console.log("App running on : " + port);
});

  

posted @ 2023-03-08 17:33  小小强学习网  阅读(432)  评论(0编辑  收藏  举报