我目前正在使用socket.io和node.js构建自己的简单多人游戏。目前,两个用户可以连接,并接收他们将占用的屏幕一侧。
我试图让客户端每按一次箭头键发送一个数据包。数据永远不会发送,或者从未被服务器接收,因为没有任何内容输出到服务器控制台。
这是我的代码: (在intex.html文件中调用init)
客户端:
var canvas
var ctx
var side = ""
var theinterval
var keys = []
var RHSpaddley = 200
var LHSpaddley = 200
var side = "null"
var socket
function init() {
canvas = document.getElementById("gameCanvas");
ctx = canvas.getContext("2d");
socket = io.connect("http://localhost:8000", { transports: ["websocket"]});
setEventHandlers();
}
function setEventHandlers() {
// Keyboard
window.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});
window.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});
// Window resize
//window.addEventListener("resize", onResize, false);
socket.on("connect", onSocketConnected);
socket.on("disconnect", onSocketDisconnect);
socket.on("new player", newPlayer)
socket.on('message', function (message) {
console.log(message);
});
socket.on('start game', startGame)
};
function onSocketConnected() {
console.log("Connected to socket server");
};
function onSocketDisconnect() {
console.log("Disconnected from socket server");
};
function newPlayer(data) {
console.log("here")
document.getElementById("title").innerHTML = data.clientsIn
console.log("clientsin is " + data.clientsIn)
if(side == "null") {
if(data.clientsIn == 1) {
side = "left"
console.log("side is left")
} else if(data.clientsIn == 2) {
side = "right"
console.log("side is right")
} else {
console.log("more than two players dectected")
}
}
}
function startGame() {
theinterval = setInterval(main, 1);
}
function main() {
doKeys()
}
function doKeys() {
//s
if(side == "left") {
console.log("left")
if(keys[40]) {
if ((LHSpaddley + 70) <= 400) {
console.log("sendingmovement")
socket.emit("LHS")
}
}
//w
if(keys[38]) {
if (LHSpaddley >= 0) {
console.log("sendingmovement")
socket.emit("LHS")
}
}
}
if(side == "right") {
console.log("right")
if(keys[38]) {
if (RHSpaddley >= 0) {
console.log("sendingmovement")
socket.emit("RHS")
}
}
//down
if(keys[40]) {
if ((RHSpaddley + 70) <=400 ) {
console.log("sendingmovement")
socket.emit("RHS")
}
}
}
}
&#13;
这是我的服务器端代码:
var util = require("util");
var express = require('express');
var app = express()
var http = require('http').Server(app);
var socket = require('socket.io')(http);
var clientsIn = 0
//game vars
var LHSpaddley = 200
var RHSpaddley = 200
var WIDTH = 600;
var HEIGHT = 400;
var ballx = 200;
var bally = 200;
var LHSpaddley = 200;
var RHSpaddley = 200;
var changex = 1;
var changey = 1;
var RHSscore = 0;
var LHSscore = 0;
var keys = [];
var hasHit = false;
var theinterval
app.get('/', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
http.listen(8000, function(){
console.log('listening on port 8000');
});
app.use(express.static('public'));
function init() {
players = [];
theinterval = setInterval(getReady, 1)
util.log("setting event handlers")
setEventHandlers()
}
init()
function setEventHandlers() {
socket.sockets.on("connection", onSocketConnection);
//socket.sockets.on("RHS", RHSmove)
//socket.sockets.on("LHS", LHSmove)
socket.sockets.on('message', function (message) {
console.log(message);
});
}
function onSocketConnection(client) {
util.log("New player has connected: "+client.id);
clientsIn++
socket.emit("new player", {clientsIn: clientsIn});
console.log("emitted");
client.on("disconnect", onClientDisconnect);
}
function onClientDisconnect() {
util.log("Player has disconnected: "+this.id);
clientsIn = clientsIn - 1
socket.sockets.emit("player left");
};
function getReady() {
if(clientsIn == 2) {
clearInterval(theinterval)
socket.sockets.emit("start game")
console.log("game started")
}
}
function LHSmove() {
console.log("LHS")
}
function RHSmove() {
console.log("RHS")
}
&#13;
谢谢!
- 博伊