Socket.io广播标志不起作用(向发送方发送数据)

时间:2017-02-20 21:37:06

标签: javascript sockets socket.io

我的客户端有{-# LANGUAGE NumDecimals #-} module Main where import Data.Foldable (for_) import System.IO import GHC.Conc (setAllocationCounter,enableAllocationLimit) main :: IO () main = do setAllocationCounter 2e9 enableAllocationLimit let writeToHandle h = for_ ([1..]::[Integer]) (hPutStrLn h . show) withFile "/dev/null" WriteMode writeToHandle return () 模块内的以下功能:

pieces

我想要实现的目的是让服务器将上述数据发送到每个连接的套接字,发送方除外。就像各种教程和official documentation所说的那样,我在服务器端放了以下代码:

        var nodeCoords = returnCoordinates(node),
        sourceCoords = returnCoordinates(sourceNode),
        board = require('board'),
        player = board.table.cells[sourceCoords.x + 'x' + sourceCoords.y].piece.owner,
        socket = io();

        $(node).css({'border': '2px solid blue', 'width' : '58px', 'height': '58px'});
        $(node).click(function(){

        board.table.cells[sourceCoords.x + 'x' + sourceCoords.y].piece.move(nodeCoords.x, nodeCoords.y);
        board.table.drawPieces();
        board.table.resetBoard();

        socket.emit('move', {'sourceCoords': sourceCoords, 'targetCoords': nodeCoords});

在我的客户端,在main requirejs文件中,事件 var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var http = require('http').Server(app); var port = 8080; var io = require('socket.io')(http); var players = { 'white': null, 'black': null } app.use(express.static('public')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); io.on('connection', function(socket){ app.get('/login', function(req, res){ if(players.white === null){ players.white = 'taken'; res.send('white'); }else if(players.white !== null && players.black === null){ players.black = 'taken'; res.send('black'); io.emit('login', {msg: 'ready'}); }else{ res.send('Two players are currently playing game. Please try later.'); } }); socket.on('move', function(data){ socket.broadcast.emit('turnReady', {'sourceCoords': data.sourceCoords, 'targetCoords': data.targetCoords}); }); socket.on('disconnect', function(){ console.log('User disconnected.'); }); }) http.listen(port, function(){ console.log('Server is listening on port ' + port + '.'); }); 看起来像:

'turnReady'

我在服务器端标志var socket = io(); $(document).ready(function() { var player; $.get('/login', function(data, status){ if(status === 'success'){ if(data === 'white' || data === 'black'){ player = data; }else{ alert(data); } } }); socket.on('login', function(data){ game(player); }); socket.on('turnReady', function(data){ console.log('ready! ' + player); board.table.cells[data.sourceCoords.x + 'x' + data.sourceCoords.y].piece.move(data.targetCoords.x, data.targetCoords.y); board.table.drawPieces(); board.table.resetBoard(); board.table.startPlayerTurn(player); }); }); 上使用,所以我希望控制台准备就绪'邮件将出现在发件人期望的每个连接的客户端上。然而,这并非如此,每个客户都会获得消息,甚至是发件人。我在这里做错了什么?

编辑:我提供了更多代码。感谢mido评论,我可以想到为什么会发生这种情况。我在件模块中创建了另一个套接字对象。这就是为什么广播没有按预期工作的原因?即使两个套接字都指向相同的默认命名空间吗?

0 个答案:

没有答案