我在unity中使用。net套接字,当我导出任何桌面操作系统的独立应用程序时,它工作得很好,即使我直接从unity项目运行应用程序,但当我使用unity web播放器时,问题就开始了。它拒绝了这个协议,我猜是因为港口。我将端口更改为我用于应用程序的端口,现在我没有拒绝客户端连接,但它仍然不起作用,显然来自客户端的数据没有到达服务器,也没有发生任何事情。Unity web播放器是否可以正常使用套接字?
public class SocketMain : MonoBehaviour {
System.Threading.Thread SocketThread;
volatile bool keepReading = false;
string alto = "", ancho ="";
int al = 0, an = 0;
// Use this for initialization
void Start () {
Application.runInBackground = true;
startServer();
}
bool bandera = false;
// Update is called once per frame
void Update () {
if(al != 0 && an != 0)
{
Grid grid = gameObject.AddComponent<Grid>();
grid.xSize = al;
grid.zSize = an;
al = 0;
an = 0;
}
}
void startServer (){
SocketThread = new System.Threading.Thread(networkCode);
SocketThread.IsBackground = true;
SocketThread.Start();
}
private string getIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
Socket listener;
Socket handler;
void networkCode()
{
string data;
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// host running the application.
Debug.Log("Ip " + getIPAddress().ToString());
IPAddress[] ipArray = Dns.GetHostAddresses("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755);
// Create a TCP/IP socket.
listener = new Socket(ipArray[0].AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
keepReading = true;
// Mientras se espera la conexion de un cliente.
Debug.Log("Esperando Conexxion");
handler = listener.Accept();
Debug.Log("Se ha Desconectado el cluiente");
data = null;
while (keepReading)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
Debug.Log("Received from Server");
if (bytesRec <= 0)
{
keepReading = false;
handler.Disconnect(true);
break;
}
//Datos recibidos y concatenados
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
//Pintamos data complto
Debug.Log(data);
var definition = new {alto = "", ancho ="" };
//Deserealizamos data
var medidas = JsonConvert.DeserializeAnonymousType(data, definition);
Debug.Log("alto :" + medidas.alto + " , ancho :" + medidas.ancho);
//Termina deserializacion
alto = medidas.alto;
ancho = medidas.ancho;
al = int.Parse(alto);
an = int.Parse(ancho);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
System.Threading.Thread.Sleep(1);
}
System.Threading.Thread.Sleep(1);
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
void stopServer()
{
keepReading = false;
//stop thread
if (SocketThread != null)
{
SocketThread.Abort();
}
if (handler != null && handler.Connected)
{
handler.Disconnect(false);
Debug.Log("Disconnected!");
}
}
void OnDisable()
{
stopServer();
}
}
这是我的服务器类
这不是说。net Sockets不工作在Unity3D WebPlayer,但多线程不工作。
http://forum.unity3d.com/threads/webplayer-multi-threading.76087/多线程在web播放器中不可用,但不是由于Unity,据我回忆,这是一个浏览器插件的安全部分,插件不能运行多个线程/生成新线程