我正在开发一款免费版 Unity 中的 android MMO android 游戏。我的电脑上安装了 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();
}
}`