限制自己使用命名空间
如果您可以控制为特定应用程序发出的所有消息和事件,则使用默认/命名空间。 如果您想利用第三方代码,或生成与其他人共享的代码,socket.io提供了一种命名空间socket的方法。
这具有多路复用单个连接的优点。 而不是使用两个WebSocket连接的socket.io,它将使用一个。
Server (app.js)
const io = require('socket.io')(80)const chat = io.of('/chat').on('connection',(socket)=>{socket.emit('a message',{that:'only','/chat':'will get'})chat.emit('a message',{everyone:'in','/chat':'will get'})})const news = io.of('/news').on('connection',socket=>{socket.emit('item',{news:'item'})})
Client (index.html)
<script>const chat = io.connect('http://localhost/chat')const news = io.connect('http://localhost/news')chat.on('connect',()=>{chat.emit('hi~')})news.on('news',()=>{news.emit('wowo~')})</script>
