如何从不同的脚本导出和发射套接字io?
mccptt671#
const { Server }= require('socket.io') class socketioserver{ constructor(){ this.io=new Server(3000,{cors: {"Access-Control-Allow-Origin": "*",methods: ["GET", "POST", "OPTIONS"]},}); this.io.on('connection',socket=>{ console.log(socket.id) }) } emit(eventName,data,id){ if(id) return this.io.to(id).emit(eventName,data) this.io.emit(eventName,data) } on(eventName){ return new Promise((resolve,reject)=>{ this.io.on('connection',(socket)=>{ socket.on(eventName,(data)=>{ resolve(data) }) }) }) } io } const ioServer=new socketioserver() ioServer.emit('apple','🍎')
const socket = io("http://localhost:3000") socket.on('connect', () => { socket.on('apple', (data) => { console.log(data) })
1条答案
按热度按时间mccptt671#
*服务器主文件
*客户端浏览器Javascript