XMLSocket 服务器端怎么弄
cyfnh 2012-09-18 04:05:08 本人想做一个Web的即时通讯功能,用Flash的XMLSocket,但对这方面没有概念,在网上找了很多资料和实例学习,但是联接就是搞不成功。也拿Adobe官方的的实例去测试,还是搞不成,提示“securityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048"” 错误 。应该是安全沙箱的问题。
在网上找到下面一段代码,但是不知道C#的代码要放在哪里,何时调用。跪求高手解答,最好说得详细、具体一点,因为本人很菜,对这个没一点概念。(找到的代码复制如下:)
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace XMLsocket
{
class Program
{
static void Main(string[] args)
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13500;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// byte[] msg = System.Text.Encoding.Default.GetBytes("Game over!!!\0");
/// stream.Write(msg, 0, msg.Length);
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
Console.WriteLine("Connected!");
NetworkStream stream = client.GetStream();
// Get a stream object for reading and writing
stream = client.GetStream();
// Set the TcpListener on port 13000.
// Buffer for reading data
int i;
Byte[] bytes = new Byte[256];
String data = null;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
//Console.WriteLine("*******"+i);
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.Default.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
if (data == "<POLICY-FILE-REQUEST/>\0")
{
string mdata = "";
System.IO.StreamReader sr = new StreamReader("crossdomain.xml");
mdata = sr.ReadToEnd();
sr.Close();
mdata = mdata + "\0";
//string mdata = "<cross-domain-policy> <allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";
byte[] msg2 = System.Text.Encoding.Default.GetBytes(mdata);
/// Send back a response.
stream.Write(msg2, 0, msg2.Length);
stream.Flush();
}
/////////////////////////////////////////////return back
if (data == "<LOGIN PASSWORD=\"XXXX\" USERNAME=\"XXXX\" />\0".ToUpper())
{
for (int ii = 0; ii < 10000; ii++)
{
data = "<MESSAGE USER=\"John\" TEXT=\"Hello, my name is John!" + System.DateTime.Now.Millisecond.ToString() + "\" />\0";
byte[] msg = System.Text.Encoding.Default.GetBytes(data);
/// Send back a response.
stream.Write(msg, 0, msg.Length);
stream.Flush();
Console.WriteLine("Sent: {0}", data);
System.Threading.Thread.Sleep(100);
}
}
}
// Shutdown and end connection
// client.Close();
}
}
catch (Exception e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}
AS:
//System.security.allowDomain("127.0.0.1");
//System.security.loadPolicyFile("http://127.0.0.1:8081/ss.xml");
//System.security.loadPolicyFile("http://127.0.0.1:8081/crossdomain.xml");
//System.security.loadPolicyFile('xmlsocket://127.0.0.1:8081');
this.createTextField("my_txt", 10, 0, 0, 300, 200);
my_txt.border = true;
my_txt.text = "Hello world";
my_txt._x = (Stage.width - my_txt._width) / 2;
my_txt._y = (Stage.height - my_txt._height) / 2;
//SOCKET服务是否已关闭
var IsScoketClose:Boolean = true;
//////////////////////////////////////////
var mySocketa = new XMLSocket();
mySocketa.addEventListener( IOErrorEvent.IO_ERROR , failConnect );
mySocketa.connect("127.0.0.1", 13500);
btn_send.onRelease = function() {
mySocketa.send(my_txt.text);
trace("send");
my_txt.text ="发送13500成功!";
};
mySocketa. = function()
{
my_txt.text =e.text;
mySocketa.close();
}
mySocketa.onConnect = function(myStatus) {
trace(myStatus);
if (myStatus) {
var my_xml:XML = new XML();
var myLogin:XMLNode = my_xml.createElement("login");
myLogin.attributes.username ="XXXX";
myLogin.attributes.password = "XXXX";
my_xml.appendChild(myLogin);
mySocketa.send(my_xml);
trace("连接13500成功!");
// mySocketa.send("Iloveyou!\n");
trace("发送13500成功!");
my_txt.text ="发送13500成功!";
} else {
trace("连接13500失败!");
my_txt.text ="连接13500失败!";
}
};
/*
mySocketa.onData = function(msg:String) {
//trace(msg);
//txtInput.text=msg;
my_txt.text =msg;
//mySocketa.send(msg);
//txtInput.text = msg;
};
*/
mySocketa.onXML = function (doc) {
var e = doc.firstChild;
if (e != null && e.nodeName == "MESSAGE") {
my_txt.text =e.attributes.USER+"/"+e.attributes.TEXT;
//displayMessage(e.attributes.user, e.attributes.text);
}
}
mySocketa.onClose = function () {
// trace("Connection to server lost.");
my_txt.text ="Connection to server lost.";
}