Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5119376
  • 博文数量: 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)

分类: Erlang

2012-08-24 07:16:00

前提:
需要下载来为ActionScript3处理JSON codec

server.erl

  1. -module(server).
  2. -export([start/0,start/1,process/1]).
  3. -define(defPort, 8888).

  4. start() -> start(?defPort).

  5. start(Port) ->
  6.   case gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]) of
  7.     {ok, LSock} -> server_loop(LSock);
  8.     {error, Reason} -> exit({Port,Reason})
  9.   end.

  10. %% main server loop - wait for next connection, spawn child to process it
  11. server_loop(LSock) ->
  12.   case gen_tcp:accept(LSock) of
  13.     {ok, Sock} ->
  14.       spawn(?MODULE,process,[Sock]),
  15.       server_loop(LSock);
  16.     {error, Reason} ->
  17.       exit({accept,Reason})
  18.   end.

  19. %% process current connection
  20. process(Sock) ->
  21.   Req = do_recv(Sock),
  22.   io:format("~p~n", [Req]),
  23.   {ok, D, []} = rfc4627:decode(Req),
  24.   {obj, [{"name", _Name}, {"age", Age}]} = D,
  25.   Name = binary_to_list(_Name),
  26.   io:format("Name: ~p, Age: ~p~n", [Name, Age]),
  27.   Resp = rfc4627:encode({obj, [{"name", 'Hideto2'}, {"age", 24}]}),
  28.   do_send(Sock,Resp),
  29.   gen_tcp:close(Sock).

  30. %% send a line of text to the socket
  31. do_send(Sock,Msg) ->
  32.   case gen_tcp:send(Sock, Msg) of
  33.     ok -> ok;
  34.     {error, Reason} -> exit(Reason)
  35.   end.

  36. %% receive data from the socket
  37. do_recv(Sock) ->
  38.   case gen_tcp:recv(Sock, 0) of
  39.     {ok, Bin} -> binary_to_list(Bin);
  40.     {error, closed} -> exit(closed);
  41.     {error, Reason} -> exit(Reason)
  42.   end.

Person.as

  1. package
  2. {
  3.     public class Person
  4.     {
  5.         public var name:String;
  6.         public var age:int;
  7.         public function Person()
  8.         {
  9.         }
  10.     }
  11. }

Client.as

  1. package {
  2.     import com.adobe.serialization.json.JSON;
  3.     
  4.     import flash.display.Sprite;
  5.     import flash.events.*;
  6.     import flash.net.Socket;
  7.     import flash.text.*;
  8.     
  9.     public class Client extends Sprite
  10.     {
  11.         private var socket:Socket;
  12.         private var myField:TextField;
  13.         private var send_data:Person;
  14.         public function Client()
  15.         {
  16.             socket = new Socket();
  17.             myField = new TextField();
  18.             send_data = new Person();
  19.             send_data.name = "Hideto";
  20.             send_data.age = 23;
  21.             socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
  22.             socket.connect("localhost", 8888);
  23.             socket.writeUTFBytes(JSON.encode(send_data));
  24.             socket.flush();
  25.             myField.x = 20;
  26.             myField.y = 30;
  27.             myField.text = "test";
  28.             myField.autoSize = TextFieldAutoSize.LEFT;
  29.             addChild(myField);
  30.         }
  31.         private function onSocketData(event:ProgressEvent):void {
  32.             while(socket.bytesAvailable) {
  33.                 var recv_data:* = JSON.decode(socket.readUTFBytes(socket.bytesAvailable));
  34.                 myField.text = "Name: " + recv_data.name + ", age: " + recv_data.age.toString();
  35.             }
  36.         }
  37.     }
  38. }
运行Erlang服务器端:

  1. Eshell> c(server).
  2. Eshell> server:start().
  3. "{\"name\":\"Hideto\",\"age\":23}"
  4. 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”

原文:http://hideto.iteye.com/blog/235811
阅读(1535) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~