Erlang与ActionScript3采用JSON格式进行Socket通讯
前提:
需要下载 as3corelib来为ActionScript3处理JSON codec
server.erl
-
module(server).
- export([start / 0 ,start / 1 ,process / 1 ]).
- define(defPort, 8888 ).
start() -> start( ? defPort).
start(Port) ->
case gen_tcp:listen(Port, [binary, {packet, 0 }, {active, false }]) of
{ok, LSock} -> server_loop(LSock);
{error, Reason} -> exit({Port,Reason})
end.
%% main server loop - wait for next connection, spawn child to process it
server_loop(LSock) ->
case gen_tcp:accept(LSock) of
{ok, Sock} ->
spawn( ? MODULE,process,[Sock]),
server_loop(LSock);
{error, Reason} ->
exit({accept,Reason})
end.
%% process current connection
process(Sock) ->
Req = do_recv(Sock),
io:format( " ~p~n " , [Req]),
{ok, D, []} = rfc4627:decode(Req),
{obj, [{ " name " , _Name}, { " age " , Age}]} = D,
Name = binary_to_list(_Name),
io:format( " Name: ~p, Age: ~p~n " , [Name, Age]),
Resp = rfc4627:encode({obj, [{ " name " , ' Hideto2 ' }, { " age " , 24 }]}),
do_send(Sock,Resp),
gen_tcp:close(Sock).
%% send a line of text to the socket
do_send(Sock,Msg) ->
case gen_tcp:send(Sock, Msg) of
ok -> ok;
{error, Reason} -> exit(Reason)
end.
%% receive data from the socket
do_recv(Sock) ->
case gen_tcp:recv(Sock, 0 ) of
{ok, Bin} -> binary_to_list(Bin);
{error, closed} -> exit(closed);
{error, Reason} -> exit(Reason)
end.
- export([start / 0 ,start / 1 ,process / 1 ]).
- define(defPort, 8888 ).
start() -> start( ? defPort).
start(Port) ->
case gen_tcp:listen(Port, [binary, {packet, 0 }, {active, false }]) of
{ok, LSock} -> server_loop(LSock);
{error, Reason} -> exit({Port,Reason})
end.
%% main server loop - wait for next connection, spawn child to process it
server_loop(LSock) ->
case gen_tcp:accept(LSock) of
{ok, Sock} ->
spawn( ? MODULE,process,[Sock]),
server_loop(LSock);
{error, Reason} ->
exit({accept,Reason})
end.
%% process current connection
process(Sock) ->
Req = do_recv(Sock),
io:format( " ~p~n " , [Req]),
{ok, D, []} = rfc4627:decode(Req),
{obj, [{ " name " , _Name}, { " age " , Age}]} = D,
Name = binary_to_list(_Name),
io:format( " Name: ~p, Age: ~p~n " , [Name, Age]),
Resp = rfc4627:encode({obj, [{ " name " , ' Hideto2 ' }, { " age " , 24 }]}),
do_send(Sock,Resp),
gen_tcp:close(Sock).
%% send a line of text to the socket
do_send(Sock,Msg) ->
case gen_tcp:send(Sock, Msg) of
ok -> ok;
{error, Reason} -> exit(Reason)
end.
%% receive data from the socket
do_recv(Sock) ->
case gen_tcp:recv(Sock, 0 ) of
{ok, Bin} -> binary_to_list(Bin);
{error, closed} -> exit(closed);
{error, Reason} -> exit(Reason)
end.
Person.as
package
{
public class Person
{
public var name:String;
public var age: int ;
public function Person()
{
}
}
}
{
public class Person
{
public var name:String;
public var age: int ;
public function Person()
{
}
}
}
Client.as
package
{
import com.adobe.serialization.json.JSON;
import flash.display.Sprite;
import flash.events. * ;
import flash.net.Socket;
import flash.text. * ;
public class Client extends Sprite
{
private var socket:Socket;
private var myField:TextField;
private var send_data:Person;
public function Client()
{
socket = new Socket();
myField = new TextField();
send_data = new Person();
send_data.name = " Hideto " ;
send_data.age = 23 ;
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
socket.connect( " localhost " , 8888 );
socket.writeUTFBytes(JSON.encode(send_data));
socket.flush();
myField.x = 20 ;
myField.y = 30 ;
myField.text = " test " ;
myField.autoSize = TextFieldAutoSize.LEFT;
addChild(myField);
}
private function onSocketData(event:ProgressEvent): void {
while (socket.bytesAvailable) {
var recv_data: * = JSON.decode(socket.readUTFBytes(socket.bytesAvailable));
myField.text = " Name: " + recv_data.name + " , age: " + recv_data.age.toString();
}
}
}
}
import com.adobe.serialization.json.JSON;
import flash.display.Sprite;
import flash.events. * ;
import flash.net.Socket;
import flash.text. * ;
public class Client extends Sprite
{
private var socket:Socket;
private var myField:TextField;
private var send_data:Person;
public function Client()
{
socket = new Socket();
myField = new TextField();
send_data = new Person();
send_data.name = " Hideto " ;
send_data.age = 23 ;
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
socket.connect( " localhost " , 8888 );
socket.writeUTFBytes(JSON.encode(send_data));
socket.flush();
myField.x = 20 ;
myField.y = 30 ;
myField.text = " test " ;
myField.autoSize = TextFieldAutoSize.LEFT;
addChild(myField);
}
private function onSocketData(event:ProgressEvent): void {
while (socket.bytesAvailable) {
var recv_data: * = JSON.decode(socket.readUTFBytes(socket.bytesAvailable));
myField.text = " Name: " + recv_data.name + " , age: " + recv_data.age.toString();
}
}
}
}
运行Erlang服务器端:
Eshell
>
c(server).
Eshell > server:start().
" {\ " name\ " :\ " Hideto\ " ,\ " age\ " :23} "
Name: " Hideto " , Age: 23
Eshell > server:start().
" {\ " name\ " :\ " Hideto\ " ,\ " age\ " :23} "
Name: " Hideto " , Age: 23
这里打印出了Erlang Socket Server接收到的AS3 Client发过来的JSON decode过的一个person对象
运行AS3客户端:
client.html上首先显示“test”,然后异步处理完Socket消息发送和接受后,decode Erlang Server端发过来的person对象,将页面上的TextField替换为“Name: Hideto2, age: 24”