0
brodcast.emit
發送消息給所有沒有套接字,當我這樣做時,節點實例崩潰,並說socket.brodcast是未定義的。Node.js Socket.io socket.brodcast未定義
這裏是我的節點代碼:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
io.on('connection', function(socket){
socket.on("newChild",childData =>{
var newChildID = mainData.newChild(childData.fatherID,childData.data, childData.type);
socket.emit("newChildID",{ "newId" : newChildID,"old" : childData.localID});
socket.brodcast.emit("newChild",maindata.getDataPoint(newChildID));
});
});
當我發出「newChild對象」來自客戶端的服務器崩潰,說socket.brodcast是不確定
的重要組成部分,是讓socket.brodcast .emit,所以我使用API錯誤? 當我後,用Google搜索,我發現這一點:Send response to all clients except sender (Socket.io)
在這個線程我發現這個例子:
socket.on('cursor', function(data) {
socket.broadcast.emit('msg', data);
});
而且它接縫像我這樣做。