简体   繁体   English

Socket.emit()和/或socket.on()不起作用

[英]Socket.emit() and/or socket.on() not working

I am making a game using node.js and socket.io and I just want to send a variable to the client using socket.emit() and socket.on(). 我正在使用node.js和socket.io进行游戏,我只想使用socket.emit()和socket.on()将变量发送给客户端。 However, this does not seem to work and I am not exactly sure why. 但是,这似乎不起作用,我不确定为什么。 I have made a condensed program to see if I am even writing this correctly, as I am very new to socket.io. 我编写了一个精简程序,以查看我是否正确编写了此程序,因为我对socket.io非常陌生。

 //server var express = require('express'); var app = express(); var serv = require('http').Server(app); app.get('/', function(req, res) { res.sendFile(__dirname + '/client/index.html'); }); app.use('/client', express.static(__dirname + '/client')); serv.listen(2000); console.log('Server started.'); var SOCKET_LIST = {}; var PLAYER_LIST = {}; var optypes = [Math.floor(Math.random()*6), Math.floor(Math.random()*6), Math.floor(Math.random()*6), Math.floor(Math.random()*6)] var op1 = { x: 450, y: 600, type: optypes[0] } socket.emit('operations', op1); 
 <canvas id="ctx" width="900" height="1400" style="border:1px solid #000000;"></canvas> <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script> //client var ctx = document.getElementById("ctx").getContext("2d"); ctx.font = '30px Arial'; var socket = io(); socket.on('operations', function(data) { console.log(data); }); </script> 

Is it the socket.emit? 是socket.emit吗? Is it the socket.on? 是socket.on吗? Is it both? 都是吗 Why am I not receiving the variable op1 in the console? 为什么我在控制台中没有收到变量op1?

Thanks in advance!!! 提前致谢!!!

You should emit after the connection . connection后,您应该发出信号。 Otherwise client is not going to receive it. 否则,客户将不会收到它。 Change your sever code with below 使用以下更改您的服务器代码

Server 服务器

var express = require('express');
var app = express();
var serv = require('http').Server(app);
var io = require('socket.io')(serv);
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));
serv.listen(2000);
console.log('Server started.');
var SOCKET_LIST = {};
var PLAYER_LIST = {};
var optypes = [Math.floor(Math.random()*6), Math.floor(Math.random()*6), Math.floor(Math.random()*6), Math.floor(Math.random()*6)]
var op1 = {
    x: 450,
    y: 600,
    type: optypes[0]
}
io.on('connection',(socket) =>{
    socket.emit('operations', op1);
})

You have not specified any socket io library in your server code. 您尚未在服务器代码中指定任何套接字io库。 In order to emit a socket you have to wait until the connection to be established. 为了发出一个套接字,您必须等待直到建立连接。

//server

var express = require('express');
var app = express();
var serv = require('http').Server(app);
var socketio = require('socket.io')(serv);

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));

serv.listen(2000);
console.log('Server started.');

var SOCKET_LIST = {};

var PLAYER_LIST = {};

var optypes = [Math.floor(Math.random()*6), Math.floor(Math.random()*6), Math.floor(Math.random()*6), Math.floor(Math.random()*6)]

var op1 = {
    x: 450,
    y: 600,
    type: optypes[0]
}

var connection = socketio
  .on('connection', function (socket) {
    socket.emit('operations', op1);
});

Also you need to specify which server you are connecting to in your client code. 另外,您还需要在客户端代码中指定要连接到的服务器。 If you are running your socket server on port 2000 you have to specify which server to connect to. 如果要在端口2000上运行套接字服务器,则必须指定要连接到的服务器。

<canvas id="ctx" width="900" height="1400" style="border:1px solid #000000;"></canvas>

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>

//client

var ctx = document.getElementById("ctx").getContext("2d");
    ctx.font = '30px Arial';

    var socket = io('http://localhost:2000');


    socket.on('operations', function(data) {
        console.log(data);        
    });

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM