如何使用 websocket 设置与 Node.js 托管服务器的统一?

问题描述 投票:0回答:1

我正在使用 unity + websocket + Node js 来玩 MMO 游戏。 我在本地 Node.js 服务器中运行它并且运行成功。但是在 openshift 中上传后我遇到了一些问题 *** Websocket 正在连接。但是通过emit发送时,并没有发送。或者我无法从服务器接收事件。

这是我的服务器代码:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

var WebSocketServer = require('ws').Server
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(200, {'Content-Type': 'text/plain'});
      response.write("Welcome to Node.js on OpenShift!\n\n");
      response.end("Thanks for visiting us! \n");
});

server.listen( port, ipaddress, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

wss = new WebSocketServer({
    server: server,
    autoAcceptConnections: false
});


wss.on('connection', function(ws) {
 console.log("Connection Success");
    var currentClient;
    var currentBot;
    var totalBot;


    ws.on('botenter', function (data) {

        console.log("Entered into botenter");
        currentBot = {
            position : data.position,  
        };

            ws.emit('BotMove', {
            position : currentBot.position,

        });

       console.log("Sending Success!!");

    });
});

console.log("Listening to " + ipaddress + ":" + port + "...");

我的客户端代码是:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SocketIO;
using System.Text.RegularExpressions;
using WebSocketSharp;

public class Onclickground : MonoBehaviour {


    private SocketIOComponent socket;
    private Transform CurrentPilot;
    public GameObject Army;
    public GameObject Box;
    private float lastClick = 0;
    private float downTime;
    // Use this for initialization
    void Start () {
        //DontDestroyOnLoad(gameObject);
        GameObject go = GameObject.Find("SocketIO");
        socket = go.GetComponent<SocketIOComponent>();
        socket.On("BotMove", BotMove);
    }

    public void BotMove(SocketIOEvent e)
    {

        //Debug.Log("I am here!");

        SetItem(Army);
        //CurrentPilot.position = new Vector3(p.x, 0, p.z);
        //Vector3 r2 = new Vector3(p.x, 0, p.z);
        //Debug.Log(e.data[0].ToString());
        Vector3 newVector;

        string[] newS = e.data[0].ToString().Split( ',');

        // attempt to parse the value using the TryParse functionality of the integer type
        string bal = newS[0];
        int memeValue2 = IntParseFast(newS[2]);
        int memeValue1 = IntParseFast(bal);
        // attempt to parse the value using the TryParse functionality of the integer type
        //int.TryParse(newS[2], out memeValue2);

        //newVector = new Vector3(float.Parse(newS[0]), 0, float.Parse(newS[2]));
        //Vector3 r2 = new Vector3(p.x, 0, p.z);

        Debug.Log(memeValue1 + " |" + memeValue2);
        Debug.Log(newS[0] + " |" + newS[2]);


        CurrentPilot.position = new Vector3(memeValue1, 0, memeValue2); 




        float angle = Mathf.Atan2(CurrentPilot.position.x, CurrentPilot.position.z) * Mathf.Rad2Deg;

        CurrentPilot.Rotate(0, angle + 180, 0);




    }

    public static int IntParseFast(string value)
    {
        int result = 0;
        int i = 0;
        int neg=1;
        for (i = 0; i < value.Length; i++)
        {
            char letter = value[i];
            if (letter == '.') break;

            if (value[i] == '-')
            {
                neg = -1;
                continue;
            }

            int disit = letter - 48;
            if(disit<=9 && disit>=0) result = 10 * result + (letter - 48);
        }
        return result*neg;
    }



    // Update is called once per frame
    void Update () {
        Vector3 m = Input.mousePosition;
        m = new Vector3(m.x, m.y, transform.position.y);
        //Debug.Log(m.x);
        Vector3 p = GetComponent<Camera>().ScreenToWorldPoint(m);

        Vector3 r1 = new Vector3(0, 0, 0);


        if (Input.GetMouseButton(0) && Time.time - lastClick > 0.2)
        {
            lastClick = Time.time;
            Debug.Log("I am here!");
            //CurrentPilot.rotation = Quaternion.Euler(r3);

            Dictionary<string, string> data = new Dictionary<string, string>();
            data["position"] = p.x.ToString() + "," + 0.ToString() + "," + p.z.ToString();
            //socket.Emit("botenter", new JSONObject(data));

        }
    }



    void SetItem(GameObject b)
    {
        CurrentPilot = ((GameObject)Instantiate(b)).transform;
    }
}
node.js unity-game-engine websocket socket.io openshift
1个回答
0
投票

https://medium.com/@6386391ritesh/unity-websocket-support-ea2420df452c

您可能会在这里找到答案,因为本文描述了如何连接到 Web 套接字服务器。

© www.soinside.com 2019 - 2024. All rights reserved.