文章来自
前一次实验,我使用的是本地swf文件,所以没有遇到安全沙箱问题,但在实际环境中我们的Flash文件是通过URL引用的,这时候Flash的Socket通讯会受到安全沙箱机制的限制。具体原理是:Flash在尝试向服务器A发起Socket连接前,会先尝试连接服务器A的843端口,求情一个xml格式的安全策略文件,该文件告诉Flash是否有权限向服务器A发起Socket连接。所以,我们需要在Erlang端增设一个守候在843端口的安全策略文件服务器。
此次实验的Erlang代码:
- -module(server).
- -export([start/1]).
- -export([policy_file_server/0, policy_file_server_proc/1]).
- -export([flash_socket_server/1, flash_socket_server_proc/1]).
- start (Port) ->
- spawn(?MODULE, policy_file_server, []),
- spawn(?MODULE, flash_socket_server, [Port]).
- %%------------------------------------------------------------------------------
- %% Flash Policy File Server
- %%------------------------------------------------------------------------------
- policy_file_server () ->
- {ok, LSock} = gen_tcp:listen(843, [binary, {packet, 0}, {active, false}]),
- policy_file_server_accept(LSock).
- policy_file_server_accept (LSock) ->
- case gen_tcp:accept(LSock) of
- {ok, Sock} ->
- spawn(?MODULE, policy_file_server_proc, [Sock]),
- policy_file_server_accept(LSock);
- {error, Reason} ->
- io:format("policy file server exit: ~s~n", [Reason]),
- exit(Reason)
- end.
- policy_file_server_proc (Sock) ->
- case gen_tcp:recv(Sock, 0) of
- {ok, _} ->
- gen_tcp:send(Sock, <<
- "1.0"?>"
- "",
- "*" to-ports="*" />",
- "",0
- >>);
- _ ->
- ok
- end,
- gen_tcp:close(Sock).
- %%------------------------------------------------------------------------------
- %% Flash Socket Server
- %%------------------------------------------------------------------------------
- flash_socket_server (Port) ->
- {ok, LSock} = gen_tcp:listen(Port, [list, {packet, 2}, {active, false}]),
- flash_socket_server_accpet(LSock).
- flash_socket_server_accpet (LSock) ->
- case gen_tcp:accept(LSock) of
- {ok, Sock} ->
- spawn(?MODULE, flash_socket_server_proc, [Sock]),
- flash_socket_server_accpet(LSock);
- {error, Reason} ->
- io:format("flash socket server exit: ~s~n", [Reason]),
- exit(Reason)
- end.
- flash_socket_server_proc (Sock) ->
- case gen_tcp:recv(Sock, 0) of
- {ok, Request} ->
- io:format("Received: ~p~n", [Request]),
- gen_tcp:send(Sock, "done!"),
- flash_socket_server_proc(Sock);
- {error, closed} ->
- io:format("Client closed~n"),
- exit(closed);
- {error, Reason} ->
- io:format("Error: ~s~n", Reason),
- exit(Reason)
- end.
在调试Flash端时,我调试了很久都没有效果,最后发现原来是浏览器缓存问题。
以下是Flash端的实验代码:
阅读(933) | 评论(0) | 转发(0) |