前一次实验,我使用的是本地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, <<
"<?xml version=\"1.0\"?>"
"<cross-domain-policy>",
"<allow-access-from domain=\"*\" to-ports=\"*\" />",
"</cross-domain-policy>",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端的实验代码:
package
{
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class Main extends Sprite
{
private var sock:Socket;
private var send:TextField;
private var input:TextField;
private var output:TextField;
public function Main ()
{
send = new TextField();
send.border = true;
send.multiline = true;
send.autoSize = TextFieldAutoSize.CENTER;
send.htmlText = "<a href='event:#'> Send </a>";
send.mouseEnabled = true;
send.selectable = false;
send.x = 320 - send.width;
send.y = 20;
send.height = 21;
input = new TextField();
input.border = true;
input.type = "input";
input.autoSize = TextFieldAutoSize.NONE;
input.x = 20;
input.y = 20;
input.width = 300 - send.width - 10;
input.height = 19;
output = new TextField();
output.border = true;
output.multiline = true;
output.autoSize = TextFieldAutoSize.NONE;
output.x = 20;
output.y = 20 + input.height + 10;
output.width = 300;
output.height = 300;
output.wordWrap = true;
addChild(send);
addChild(input);
addChild(output);
sock = new Socket();
sock.addEventListener(
ProgressEvent.SOCKET_DATA,
function (event:ProgressEvent):void
{
/* 与Erlang端{packet, 0}对应的代码
while (sock.bytesAvailable)
{
var s:String = sock.readUTFBytes(sock.bytesAvailable);
output.appendText(s);
}
*/
/* 与Erlang端{packet, 0}对应的代码 */
var s:String = sock.readUTF();
input.text = "";
output.appendText(s);
output.appendText("\r\n");
}
);
sock.connect("localhost", 10086);
sock.flush();
send.addEventListener(
MouseEvent.CLICK,
function (event:MouseEvent):void
{
/* 与Erlang端{packet, 0}对应的代码
sock.writeUTFBytes("Hello World!");
*/
/* 与Erlang端{packet, 0}对应的代码 */
sock.writeUTF(input.text);
sock.flush();
}
);
}
}
}
分享到:
相关推荐
- 使用yum安装Erlang:`sudo yum install erlang-23.3.4.3-1.el7.x86_64.rpm` 2. **安装RabbitMQ**: - 下载RabbitMQ RPM包:`wget https://your_download_link/rabbitmq-server-3.8.17-1.el7.noarch.rpm` - ...
erlang-20.3.8.17-1.el7.centos.x86_64
erlang-sd_notify-1.0-2.el7.x86_64.rpm
erlang-xmerl-22.3-1.el7.x86_64.rpm,rabbitMQ安装需要依赖此环境。Erlang 是一种多用途编程语言,主要用于开发并发和分布式系统。它最初是一种专有的编程语言,Ericsson 使用它来开发电话和通信应用程序。
erlang-xmerl-23.0.2-2.el7.x86_64.rpm,rabbitMQ安装需要依赖此环境。Erlang 是一种多用途编程语言,主要用于开发并发和分布式系统。它最初是一种专有的编程语言,Ericsson 使用它来开发电话和通信应用程序。
erlang-21.3.8.11-1.el6.x86_64.rpm
erlang-19.3.6.4-1.el
https://blog.51cto.com/7794482/2436678 可根据文档进行部署,redis+mysql+mq的插件 rabbitmq 安装时需要该插件
erlang-21.3.8.15-1.el7.x86_64.rpm
erlang-21.3.7.1-1.el7.x86_64.rpm rabbitmq基础语言环境。
对应rabbitmq 3.12.4,亲测可用
erlang-20.3.6-1.el7.centos.x86_64.rpm erlang-20.3.6-1.el7.centos.x86_64.rpm erlang-20.3.6-1.el7.centos.x86_64.rpm erlang-20.3.6-1.el7.centos.x86_64.rpm erlang-20.3.6-1.el7.centos.x86_64.rpm
esl-erlang_24.2.1-1_centos_7_amd64.rpm
linux基于centos7.x,erlang21.3.8.16资源适配rabbitmq3.8.5。欢迎大家下载!!!!
erlang-23.2.7-1.el7.x86_64 .rpm
配套rabbitmq-server-3.8.17-1.el8.noarch.rpm
erlang-23.2.6-1.el7.x86_64
rabbitmq安装前需要安装erlang包,如果没有erlang包,rabbitmp则无法下载成功
由于不同版本的rabbitmq需要的erlang版本不一样,但是官网已经无法下载,此版本的erlang适用于rabbitmq3.7.4-3.7.8,其余版本自查是否可用
erlang-erl_interface-19.3.6.4-1.el7.x86_64.rpm