[转载]利用Flash XMLSocket实现”服务器推”技术

[转载]利用Flash XMLSocket实现”服务器推”技术 – 无风 – 博客园.

利用Flash XML Socket实现”服务器推”技术的基础是:1.Flash提供了XMLSocket类,服务器利用Socket向Flash发送数据;2.JavaScript和Flash的紧密结合,JavaScript和as可以相互调用。

具体实现的方法:在HTML页面中陷入一个使用了XMLSocket类的Falsh程序。JavaScript通过调用此Flash程序提供的套接口接口 与服务端的套接口进行通信。JavaScript在收到服务器端以XML格式传送的信息控制HTML的Dom对象改变页面的内容显示。

image

一、XMLSocket简介

XMLSocket 类实现了客户端套接字,这使得运行 Flash Player 的计算机可以与由 IP 地址或域名标识的服务器计算机进行通信。 对于要求滞后时间较短的客户端/服务器应用程序,如实时聊天系统,XMLSocket 类非常有用。 传统的基于 HTTP 的聊天解决方案频繁轮询服务器,并使用 HTTP 请求来下载新的消息。 与此相对照,XMLSocket 聊天解决方案保持与服务器的开放连接,这一连接允许服务器即时发送传入的消息,而无需客户端发出请求。 若要使用 XMLSocket 类,服务器计算机必须运行可识别 XMLSocket 类使用的协议的守护程序。可以参考XMLSocket简介

二、AS与JavaScript通信

利用ExternalInterface可以实现AS与JavaScript的通信,具体请看Flex与JavaScript交互

三、实现Socket

客户端Flex:

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3:      <mx:Script>
 4:          <![CDATA[
 5:              private var socket:XMLSocket=new XMLSocket();
 6:  
 7:              private function ConncetServer():void
 8:              {
 9:                  socket.addEventListener(DataEvent.DATA,OnReceived);
10:                  socket.addEventListener(Event.CONNECT , onConnected);
11:                  this.socket.connect("127.0.0.1",8080);
12:              }
13:  
14:              private function onConnected(event:Event):void
15:              {
16:                  socket.send("begin connect");
17:                  this.txtContent.text=this.txtContent.text+"conect success\n";
18:              }
19:  
20:              private function Send():void
21:              {
22:                  socket.send("message from flex");
23:              }
24:  
25:               private function OnReceived(event:DataEvent):void
26:               {
27:                  trace("receive data.");
28:                  this.txtContent.text=this.txtContent.text+"\n"+ event.text;
29:               }
30:          ]]>
31:      </mx:Script>
32:      
33:      <mx:Button label="Connect" fontSize="12" id="btnConnect" left="10" top="20" click="ConncetServer()"/>
34:      <mx:Button label="Send" fontSize="12" id="btnSend" left="80" top="20" click="Send();" />
35:      <mx:TextArea borderColor="#010A10" id="txtContent" left="10" top="110" right="10" bottom="10" backgroundColor="#DEEEF3" cornerRadius="12"/>
36:  </mx:Application>
.codearea { color: black; background-color: white; line-height: 18px; border: 1px solid rgb(79, 129, 189); margin: 0pt; width: auto ! important; overflow: auto; text-align: left; font-size: 12px; font-family: “Courier New”,”Consolas”,”Fixedsys”,”BitStream Vera Sans Mono”,courier,monospace,serif; }.codearea pre { color: black; line-height: 18px; padding: 0pt 0pt 0pt 12px ! important; margin: 0em; background-color: rgb(255, 255, 255) ! important; }.linewrap pre { white-space: pre-wrap; word-wrap: break-word; }.codearea pre.alt { background-color: rgb(247, 247, 255) ! important; }.codearea .lnum { color: rgb(79, 129, 189); line-height: 18px; }

Socket服务端:

  1:  using System;
  2:  using System.Collections.Generic;
  3:  using System.Linq;
  4:  using System.Text;
  5:  using System.Net;
  6:  using System.Net.Sockets;
  7:  using System.Threading;
  8:  
  9:  namespace SocketServer
 10:  {
 11:      class Program
 12:      {
 13:          static bool ServiceStartFlag = false;
 14:          static Socket socket;
 15:          static Thread thread;
 16:  
 17:          static void Main(string[] args)
 18:          {
 19:              socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 20:              IPHostEntry ieh = Dns.GetHostEntry("localhost");
 21:              IPAddress localServerIP = ieh.AddressList[1];
 22:              IPEndPoint localIPEndPoint = new IPEndPoint(localServerIP, 8080);
 23:  
 24:              socket.Bind(localIPEndPoint);
 25:              socket.Listen(600);
 26:  
 27:              thread = new Thread(new ThreadStart(AcceptClient));
 28:              thread.IsBackground = true;
 29:              thread.Start();
 30:  
 31:              Console.ReadLine();
 32:          }
 33:  
 34:          static void AcceptClient()
 35:          {
 36:              ServiceStartFlag = true;
 37:  
 38:              while (ServiceStartFlag)
 39:              {
 40:                  try
 41:                  {
 42:                      Socket newSocket = socket.Accept();
 43:                      string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "*" + "\" to-ports=\"8080\"/></cross-domain-policy>\0";
 44:  
 45:                      byte[] tmpBytes = Encoding.UTF8.GetBytes(onemessge);
 46:                      newSocket.Send(tmpBytes);
 47:  
 48:                      Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
 49:                      newThread.IsBackground = true;
 50:                      object obj = newSocket;
 51:                      newThread.Start(obj);
 52:                  }
 53:                  catch (SocketException ex)
 54:                  {
 55:  
 56:  
 57:  
 58:                  }
 59:              }
 60:          }
 61:  
 62:          static void ReadMsg(object obj)
 63:          {
 64:              Socket socket = (Socket)obj;
 65:  
 66:              byte[] byteMessage = null; ;
 67:  
 68:              while (ServiceStartFlag)
 69:              {
 70:                  try
 71:                  {
 72:                      if (socket.Connected)
 73:                      {
 74:  
 75:                          byteMessage = new byte[1000];
 76:                          int len = socket.Receive(byteMessage);
 77:                          if (len > 0)
 78:                          {
 79:                              string sTime = DateTime.Now.ToShortTimeString();
 80:  
 81:                              string msg = sTime + ":" + "Message from:";
 82:  
 83:                              msg += socket.RemoteEndPoint.ToString() + Encoding.UTF8.GetString(byteMessage);
 84:                              Console.WriteLine(msg);
 85:                              byteMessage = null;
 86:  
 87:                              byte[] tmpBytes = Encoding.UTF8.GetBytes("Sended Sucessed!\0");
 88:  
 89:                              socket.Send(tmpBytes);
 90:                          }
 91:  
 92:  
 93:                      }
 94:                  }
 95:                  catch (SocketException ex)
 96:                  {
 97:                      Console.WriteLine(ex.Message);
 98:                  }
 99:              }
100:          }
101:      }
102:  }
103:  
104:  

.codearea { color: black; background-color: white; line-height: 18px; border: 1px solid rgb(79, 129, 189); margin: 0pt; width: auto ! important; overflow: auto; text-align: left; font-size: 12px; font-family: “Courier New”,”Consolas”,”Fixedsys”,”BitStream Vera Sans Mono”,courier,monospace,serif; }.codearea pre { color: black; line-height: 18px; padding: 0pt 0pt 0pt 12px ! important; margin: 0em; background-color: rgb(255, 255, 255) ! important; }.linewrap pre { white-space: pre-wrap; word-wrap: break-word; }.codearea pre.alt { background-color: rgb(247, 247, 255) ! important; }.codearea .lnum { color: rgb(79, 129, 189); line-height: 18px; }

运行结果:

image

image

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏