我写的 C# 代码已经在本地服务器端运行。
System.Net.Sockets.Socket server = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
server.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 4502));
server.Listen(int.MaxValue);
while (true)
{
System.Net.Sockets.Socket client = server.Accept();
if (client.Connected)
client.Send(new byte[1]);
}
然后在本地网页上构建一个嵌入html文件的swf文件——127.0.0.1/Socket.htm
public class WebSocket extends flash.display.Sprite
{
private var Socket:flash.net.Socket = new flash.net.Socket();
public function WebSocket()
{
flash.system.Security.allowDomain("*");
flash.system.Security.allowInsecureDomain("*");
flash.system.Security.loadPolicyFile("PolicyFile.xml");
this.Socket.addEventListener( flash.events.Event.CONNECT, Socket_Connect );
this.Socket.connect("127.0.0.1",4502);
}
private function Socket_Connect(event:Event):void
{
this.MyLabel.text= "The socket is now connected..." ;
}
}
这是“127.0.0.1/PolicyFile.xml”的内容:
<?xml version="1.0" encoding="utf-8"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
代码运行没有任何错误,但永远不会触发 CONNECT 事件。
感谢您提前提供任何帮助!