[英]Socket.IO Broadcast emit not working how to fix it ,
Hey there i'm trying to send message to everyone connected to the server that a new user is connected except the new user connect so i'm using socket.broadcast.emit() but it doesn't fire up in browser console嘿,我正在尝试向连接到服务器的每个人发送消息,除了新用户连接之外,新用户已连接,所以我正在使用 socket.broadcast.emit() 但它不会在浏览器控制台中启动
my code:我的代码:
const path = require('path')
const http = require('http')
const express = require('express')
const socketio = require('socket.io')
const app = express()
const server = http.createServer(app)
const io = socketio(server)
const port = process.env.PORT || 3000
const publicDirectoryPath = path.join(__dirname,'../public')
app.use(express.static(publicDirectoryPath))
io.on('connection',(socket)=>{
console.log('New web socket connection')
socket.emit('welcome',"Welcome !")
socket.broadcast.emit('message','A new user has joined !')
socket.on('sendMessage',(message)=>{
io.emit('messageSent',message)
})
})
server.listen(port,()=>{
console.log('Server is up on 3000')
})
You can send to everyone (including yourself) with:您可以发送给所有人(包括您自己):
io.emit(...)
Or to everyone else (but not yourself - ie not socket
) with:或者对其他所有人(但不是你自己 - 即不是
socket
):
socket.broadcast.emit(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.