我想为我的Unity应用程序建立一个websocket控制服务器。 HTML客户端应发送包含目标和突击队的JSON代码。服务器应解析目标和突击队,并应将其发送到特定客户端,反之亦然。
我的问题是:我无法将连接与客户端结合起来......就像连接[用户]一样,并使用json消息中包含的用户名来解决此问题。在这种情况下,互联网上没有太多有用的例子。特别是对于正确的语法
希望有人可以帮助我: - )
//===============================================
// Websocket Command Server 1.0
//===============================================
// Websocket-Server
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({host: 'localhost',port: 1337});
console.log('Server gestartet...');
wss.on('connection', function(ws)
{
var userName = false;
console.log('client verbunden...');
ws.on('message', function(message)
{
if (userName === false) { // first message sent by user is their name
// remember user name
userName = message;
console.log((new Date()) + ' Received Message from ' + userName + ': ' + message);
}
else { // log and broadcast the message
var obj = JSON.parse(message);
if("User" in obj) {
// New client, add it to the id/client object
var targetuser = obj.user;
console.log('targetuser: ' + obj.user);
}
if ("Command" in obj){
// Send data to the client requested
var command = obj.command;
console.log('Command: ' + obj.command);
}
else {
console.log('nix im json');
}
targetuser.send(command);
}});
});