Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5119710
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Java

2013-01-01 16:00:53

文章来自

前一次实验,我使用的是本地swf文件,所以没有遇到安全沙箱问题,但在实际环境中我们的Flash文件是通过URL引用的,这时候Flash的Socket通讯会受到安全沙箱机制的限制。具体原理是:Flash在尝试向服务器A发起Socket连接前,会先尝试连接服务器A的843端口,求情一个xml格式的安全策略文件,该文件告诉Flash是否有权限向服务器A发起Socket连接。所以,我们需要在Erlang端增设一个守候在843端口的安全策略文件服务器。

此次实验的Erlang代码:


 

  1. -module(server).
  2. -export([start/1]).
  3. -export([policy_file_server/0, policy_file_server_proc/1]).
  4. -export([flash_socket_server/1, flash_socket_server_proc/1]).

  5. start (Port) ->
  6.     spawn(?MODULE, policy_file_server, []),
  7.     spawn(?MODULE, flash_socket_server, [Port]).


  8. %%------------------------------------------------------------------------------
  9. %% Flash Policy File Server
  10. %%------------------------------------------------------------------------------

  11. policy_file_server () ->
  12.     {ok, LSock} = gen_tcp:listen(843, [binary, {packet, 0}, {active, false}]),

  13.     policy_file_server_accept(LSock).

  14. policy_file_server_accept (LSock) ->
  15.     case gen_tcp:accept(LSock) of
  16.         {ok, Sock} ->
  17.             spawn(?MODULE, policy_file_server_proc, [Sock]),

  18.             policy_file_server_accept(LSock);

  19.         {error, Reason} ->
  20.             io:format("policy file server exit: ~s~n", [Reason]),
  21.             exit(Reason)
  22.     end.

  23. policy_file_server_proc (Sock) ->
  24.     case gen_tcp:recv(Sock, 0) of
  25.         {ok, _} ->
  26.             gen_tcp:send(Sock, <<
  27.                 "1.0"?>"
  28.                 "",
  29.                 "*" to-ports="*" />",
  30.                 "",0
  31.             >>);
  32.         _ ->
  33.             ok
  34.     end,
  35.     gen_tcp:close(Sock).

  36. %%------------------------------------------------------------------------------
  37. %% Flash Socket Server
  38. %%------------------------------------------------------------------------------

  39. flash_socket_server (Port) ->
  40.     {ok, LSock} = gen_tcp:listen(Port, [list, {packet, 2}, {active, false}]),

  41.     flash_socket_server_accpet(LSock).

  42. flash_socket_server_accpet (LSock) ->
  43.     case gen_tcp:accept(LSock) of
  44.         {ok, Sock} ->
  45.             spawn(?MODULE, flash_socket_server_proc, [Sock]),

  46.             flash_socket_server_accpet(LSock);

  47.         {error, Reason} ->
  48.             io:format("flash socket server exit: ~s~n", [Reason]),
  49.             exit(Reason)
  50.     end.

  51. flash_socket_server_proc (Sock) ->
  52.     case gen_tcp:recv(Sock, 0) of
  53.         {ok, Request} ->
  54.             io:format("Received: ~p~n", [Request]),

  55.             gen_tcp:send(Sock, "done!"),

  56.             flash_socket_server_proc(Sock);

  57.         {error, closed} ->
  58.             io:format("Client closed~n"),
  59.             exit(closed);

  60.         {error, Reason} ->
  61.             io:format("Error: ~s~n", Reason),
  62.             exit(Reason)
  63.     end.

在调试Flash端时,我调试了很久都没有效果,最后发现原来是浏览器缓存问题。

以下是Flash端的实验代码:


 

  1. package
  2. {
  3.     import flash.events.MouseEvent;
  4.     import flash.events.ProgressEvent;
  5.     import flash.net.Socket;
  6.     import flash.display.Sprite;
  7.     import flash.text.TextField;
  8.     import flash.text.TextFieldAutoSize;

  9.     public class Main extends Sprite
  10.     {
  11.         private var sock:Socket;

  12.         private var send:TextField;
  13.         private var input:TextField;
  14.         private var output:TextField;

  15.         public function Main ()
  16.         {
  17.             send = new TextField();
  18.             send.border = true;
  19.             send.multiline = true;
  20.             send.autoSize = TextFieldAutoSize.CENTER;
  21.             send.htmlText = " Send ";
  22.             send.mouseEnabled = true;
  23.             send.selectable = false;

  24.             send.x = 320 - send.width;
  25.             send.y = 20;
  26.             send.height = 21;

  27.             input = new TextField();
  28.             input.border = true;
  29.             input.type = "input";
  30.             input.autoSize = TextFieldAutoSize.NONE;

  31.             input.x = 20;
  32.             input.y = 20;
  33.             input.width = 300 - send.width - 10;
  34.             input.height = 19;

  35.             output = new TextField();
  36.             output.border = true;
  37.             output.multiline = true;
  38.             output.autoSize = TextFieldAutoSize.NONE;

  39.             output.x = 20;
  40.             output.y = 20 + input.height + 10;
  41.             output.width = 300;
  42.             output.height = 300;
  43.             output.wordWrap = true;

  44.             addChild(send);
  45.             addChild(input);
  46.             addChild(output);

  47.             sock = new Socket();

  48.             sock.addEventListener(
  49.                 ProgressEvent.SOCKET_DATA,
  50.                 function (event:ProgressEvent):void
  51.                 {
  52.                     /* 与Erlang端{packet, 0}对应的代码
  53.                 while (sock.bytesAvailable)
  54.                     {
  55.                         var s:String = sock.readUTFBytes(sock.bytesAvailable);

  56.                         output.appendText(s);
  57.                     }
  58.                     */

  59.                     /* 与Erlang端{packet, 0}对应的代码 */
  60.                     var s:String = sock.readUTF();

  61.                     input.text = "";

  62.                     output.appendText(s);
  63.                     output.appendText("rn");
  64.                 }
  65.             );

  66.             sock.connect("localhost", 10086);

  67.             sock.flush();

  68.             send.addEventListener(
  69.                 MouseEvent.CLICK,
  70.                 function (event:MouseEvent):void
  71.                 {
  72.                     /* 与Erlang端{packet, 0}对应的代码
  73.                     sock.writeUTFBytes("Hello World!");
  74.                     */

  75.                     /* 与Erlang端{packet, 0}对应的代码 */
  76.                     sock.writeUTF(input.text);

  77.                     sock.flush();
  78.                 }
  79.             );
  80.         }
  81.     }
  82. }



 

阅读(933) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~