【问题标题】:Creating a websocket for an android MMO using Unity3d and Node.js使用 Unity3d 和 Node.js 为 android MMO 创建 websocket
【发布时间】:2014-09-24 06:51:25
【问题描述】:

我正在开发一款免费版 Unity 中的安卓 MMO 安卓游戏。我的电脑上安装了 node.js。我应该使用以下服务器客户端代码吗,因为当我尝试测试它时,我可以让服务器端脚本运行,但无法确定客户端是否正在连接。此外,当我尝试构建 apk 时,unity 会给出错误消息“使用 System.Net.Sockets 需要专业版的 unity”。请帮忙!!

服务器端(在 node.js 上)

`var net = require('net');
var tcp_server = net.createServer(function(socket)
{

    socket.write('hello\n');
    socket.end('world\n');
});
tcp_server.listen(8000);
console.log("Server Running...");`

客户端(在统一 c# 脚本中)

`using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Text;
public class tcpClient : MonoBehaviour 
{
    static void Main(string[] args)
    {
        Debug.Log("start");
        TcpClient tcpClient = new TcpClient();
        tcpClient.Connect("127.0.0.1", 8000);
        Debug.Log ("Connected");
        NetworkStream clientStream = tcpClient.GetStream();
        byte[] message = new byte[4096];
        int bytesRead;
        bytesRead = 0;

        try
        {
            // Read up to 4096 bytes
            bytesRead = clientStream.Read(message, 0, 4096);
        }
        catch
        {
            /*a socket error has occured*/
        }
        //We have read the message.
        ASCIIEncoding encoder = new ASCIIEncoding();
        Debug.Log(encoder.GetString(message, 0, bytesRead));
        //Console.WriteLine(encoder.GetString(message, 0, bytesRead));
        tcpClient.Close();
    }
}`

【问题讨论】:

    标签: android node.js unity3d websocket mmo


    【解决方案1】:

    最简单的方法可能是使用 socket.io

    http://socket.io/get-started/

    【讨论】:

      【解决方案2】:

      首先,如果你想使用原生套接字,你需要 Unity3D Pro。

      但是还有其他方法可以解决这个问题。

      您可以使用其中之一。您可以在 Unity Asset Store 中找到它们。(我知道这些可能比这些资产更多)。 (它们不是免费的,但比 Unity pro 便宜)。

      1. Good ol' Sockets
      2. Best HTTP Basic
      3. Best HTTP Pro Edition

      在那之后@ploutch 提到了Socket.IO。真的很不错。

      但不要忘记 Unity3D 有它自己的 Network。您可以创建服务器也可以使用它加入客户端。还有一些非常好的供应商,并为您提供了额外的选择。

      喜欢:

      1. Photon Unity Networking
      2. Bolt Networking Middleware

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-18
        • 2017-05-25
        • 2015-03-31
        • 2015-01-16
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多