我正在開發免費版本的Unity中的android MMO android遊戲。我的電腦上安裝了node.js。我應該使用以下服務器 - 客戶端代碼,因爲當我試圖測試它時,我可以運行服務器端腳本,但無法確定客戶端是否連接。另外,當我試圖構建apk時,Unity給出了一個錯誤,說「使用System.Net.Sockets需要專業版的統一。」請幫忙!!使用Unity3d和Node.js爲android MMO創建websocket
服務器端(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();
}
}`