• Erlang与ActionScript3采用JSON格式进行Socket通讯


    http://hideto.iteye.com/blog/235811

    需要下载as3corelib来为ActionScript3处理JSON codec 

    server.erl 

    Java代码  收藏代码
    1. -module(server).  
    2. -export([start/0,start/1,process/1]).  
    3. -define(defPort, 8888).  
    4.   
    5. start() -> start(?defPort).  
    6.   
    7. start(Port) ->  
    8.   case gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]) of  
    9.     {ok, LSock} -> server_loop(LSock);  
    10.     {error, Reason} -> exit({Port,Reason})  
    11.   end.  
    12.   
    13. %% main server loop - wait for next connection, spawn child to process it  
    14. server_loop(LSock) ->  
    15.   case gen_tcp:accept(LSock) of  
    16.     {ok, Sock} ->  
    17.       spawn(?MODULE,process,[Sock]),  
    18.       server_loop(LSock);  
    19.     {error, Reason} ->  
    20.       exit({accept,Reason})  
    21.   end.  
    22.   
    23. %% process current connection  
    24. process(Sock) ->  
    25.   Req = do_recv(Sock),  
    26.   io:format("~p~n", [Req]),  
    27.   {ok, D, []} = rfc4627:decode(Req),  
    28.   {obj, [{"name", _Name}, {"age", Age}]} = D,  
    29.   Name = binary_to_list(_Name),  
    30.   io:format("Name: ~p, Age: ~p~n", [Name, Age]),  
    31.   Resp = rfc4627:encode({obj, [{"name", 'Hideto2'}, {"age", 24}]}),  
    32.   do_send(Sock,Resp),  
    33.   gen_tcp:close(Sock).  
    34.   
    35. %% send a line of text to the socket  
    36. do_send(Sock,Msg) ->  
    37.   case gen_tcp:send(Sock, Msg) of  
    38.     ok -> ok;  
    39.     {error, Reason} -> exit(Reason)  
    40.   end.  
    41.   
    42. %% receive data from the socket  
    43. do_recv(Sock) ->  
    44.   case gen_tcp:recv(Sock, 0) of  
    45.     {ok, Bin} -> binary_to_list(Bin);  
    46.     {error, closed} -> exit(closed);  
    47.     {error, Reason} -> exit(Reason)  
    48.   end.  



    Person.as 

    Java代码  收藏代码
    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 

    Java代码  收藏代码
    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服务器端: 

    Java代码  收藏代码
    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”

  • 相关阅读:
    《分布式之数据库缓存双写一致性方案解析》
    淘系工程师讲解的使用Spring特性优雅书写业务代码
    简述BIO/NIO/AIO前世今生
    经典面试题:分布式缓存热点KEY问题如何解决有赞方案
    软件中的文本本地化
    Java Instant\Date\LocalDateTime\Calendar\ZonedDateTime转化
    java反序列化漏洞专项
    web打印样式预览调试技巧
    公钥私钥 与 http/https
    docker安装mysql
  • 原文地址:https://www.cnblogs.com/fvsfvs123/p/4192029.html
Copyright © 2020-2023  润新知