日期:2014-05-16 浏览次数:20472 次
霸哥的blog中提到过Erlang服务器在通信中与客户端的数据交换方式:yufeng
为了简单起见这个聊天室程序采用json,要使用到rfc4627 这个库
先定义一个Message类:
?
public class Message { String id; //消息ID String type; //消息类型 String from; //发送方 String to; //接收方 String subject; //主题 String content; //内容 Date creationDate; //时间 public Message(String type,String from,String to,String subject,String content){ this.id =MessageIdGenerator.nextId(); this.type=type; this.from=from; this.to=to; this.subject=subject; this.content=content; this.creationDate=new Date(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getCreationDate() { return creationDate; } public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } } 再定义一个JSON的工具类:
public class JSONParaser { public static JSONObject getJSON(Object content){ try{ JSONObject result = JSONObject.fromObject(content); return result; } catch(Exception ex){ return null; } } public static Object getString(String json){ try{ JSONObject jobj=JSONObject.fromObject(json); return JSONObject.toBean(jobj); } catch(Exception ex){ return null; } } } 再修改SOCKET 发送部分代码:
public void sendMsg(Message msg){ try { String data=(JSONParaser.getJSON(msg)).toString(); oust.write(data.getBytes()); oust.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 测试下发送后服务器端接收到的数据:
client_session dwmsg recived {message,undefined,msg, [6], undefined,undefined, <<"{\"content\":\"aaaaa\",\"creationDate\":{\"date\":27,\"day\":1,\"hours\":13,\"minutes\":10,\"month\":1,\"seconds\":26,\"time\":1330319426281,\"timezoneOffset\":-480,\"year\":112},\"from\":\"client1\",\"id\":\"x8yL-2\",\"subject\":\"chat\",\"to\":\"\",\"type\":\"msg\"}">>, undefined} client_session dwmsg sended
服务器端新建一个模块util_MessageParas,将收到的json数据转成内部可识的message:
%% Author: Administrator %% Created: 2012-2-27 %% Description: TODO: Add description to util_MessageParas -module(util_MessageParas). %% %% Include files %% %%-include("json.hrl"). -include("message.hrl"). %% %% Exported Functions %%<<"{\"content\":\"aaa\",\"creationDate\":{\"date\":27,\"day\":1,\"hours\":18,\"minutes\":8,\"month\":1,\"seconds\":26,\"time\":1330337306984,\"timezoneOffset\":-480,\"year\":112},\"from\":\"client1\",\"id\":\"289n-2\",\"subject\":\"chat\",\"to\":\"\",\"type\":\"msg\"}">> -export([paraseDecode/1]). %% %% API Functions %% %% %% Local Functions %% %paras json data to message paraseDecode(Bin)-> case rfc4627:decode(Bin) of {ok,Obj,_Re}-> paraElements(Obj); {error,Reason}-> {error,Reason} en