四、XMLSocket
这是LoadVars(XML)、Flash Remoting、Webservice、XMLSocket四种方法整理的最后一篇,也让大家久等了(没想到前几篇的文章在网上挺受欢迎的,其中还有一人给我发邮件,相看我这个最后一篇,哈哈,还是挺欣慰的。对转载我要声明一下,首先这几篇文章欢迎转载的,但要说明文章的作者,以及文章的原址吧,我发现有些网站转载,连作者都不写了或者写的就不对。这一点会影响我以后写文章的心情的,特此说明一下!)。现在接下来转入正题了!
XMLSocket主要用于与服务端进行即时通信,目前的应用领域主要是Flash文本聊天和Flash在线游戏等方面。
XMLSocket的优点:
1、能和服务端即时通信;
2、Flash Player 5.0以上的版本内置类,不需另装组件或插件;
3、因为XMLSocket就是相当于一个Socket客户端,所以一般的中间件都支持的(如java,.Net等)
XMLSocket的缺点:
1、XMLSocket只能传字符串或xml格式的文本,数据类型单一;
2、XMLSocket服务端自行开发的话,需要对Socket技术比较了解才行,好在网上有现成的服务端软件(商业的XMLSocket Server 有Unity、Fortress;开源的XMLSocket Server 有Oregano Multiuser Server);
3、还有就是XMLSocket的80端口与flash安全策略问题。(网上有一个解决方法,不知是否可行,请自行验证)
//=======================================; if (msg.charCodeAt(0) == 13 && msg.charCodeAt(1) == 10) { msg = msg.substr(2); } return msg.split(paramObj.CommandDelimiters); } |
//上面与XMLSocket有关的主要代码,显示方面自己添加相关组件就行了!
//有一个注意点,在flash向服务端发送的命令的最后一定要加上“\r”,否则服务端无法收到消息(我的服务端是用Java开发的)
//=======================================; // 服务端代码(我用java开发的,其他版本自行研究); // ChatServer.java //=======================================; package com.klstudio.socket.chat; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Vector; //import com.klstudio.util.Logger; /** * @author kinglong * * TODO 要更改此生成的类型注释的模板,请转至窗口-首选项- Java -代码样式-代码模板 */ public class ChatServer { //private Logger logger; private static Vector clients = new Vector(); private static ServerSocket server = null; private static Socket socket = null; public static String CommandDelimiters = "-@@##@@-"; public static String PeopleDelimiters = "-@#@-"; public ChatServer() { } public static void notifyRoom() { StringBuffer people = new StringBuffer("PEOPLE"+CommandDelimiters+"所有的人"); for (int i = 0; i < clients.size(); i++) { Client client = (Client) clients.elementAt(i); people.append(PeopleDelimiters+client.getClientName()); } sendClients(people); } public staticboolean checkName(Client newClient){ for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); if(client != newClient && client.getClientName().equals(newClient.getClientName())){ return false; } } return true; } public static void closeAll(){ while(clients.size()>0){ Client client = (Client) clients.firstElement(); try { client.getClientSocket().close(); } catch (IOException e) { // TODO 自动生成 catch 块 //Logger logger = new Logger(System.out); //logger.log("错误-" + e.toString()); } finally { clients.removeElement(client); } } } public static synchronized void disconnect(Client client) { client.send(new StringBuffer("QUIT")); try { client.getClientSocket().close(); } catch (IOException e) { // TODO 自动生成 catch 块 //Logger logger = new Logger(System.out); //logger.log("错误-" + e.toString()); } finally{ clients.removeElement(client); } } public static synchronized void sendClients(StringBuffer sb) { for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); client.send(sb); } } public static synchronized void sendClients(StringBuffer sb,String ownerName,String toName) { for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); if(toName.equals(client.getClientName()) || toName.equals("所有的人") || ownerName.equals(client.getClientName())){ client.send(sb); } } } public static synchronized void sendClients(Client ownerClient) { for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); if(client.getClientName().equals(ownerClient.getClientName())){ client.send(new StringBuffer("MSG"+CommandDelimiters+"系统信息>欢迎你进入!")); }else{ client.send(new StringBuffer("MSG"+CommandDelimiters+"系统信息>["+ownerClient.getClientName()+"]用户进入!")); } } } public static void main(String[] args) { int port = 8888; if(args.length>0){ port = Integer.parseInt(args[0]); } //Logger logger = new Logger(System.out); //logger.log("信息-ChatServer["+port+"]服务正在启动..."); try { server = new ServerSocket(port); } catch (IOException e) { // TODO 自动生成 catch 块 //logger.log("错误-"+e.toString()); } while(true){ if(clients.size()<5){ try { socket = server.accept(); if(socket != null){ //logger.log("信息-"+socket.toString()+"连接"); } } catch (IOException e) { // TODO 自动生成 catch 块 //logger.log("错误-"+e.toString()); } int i=0; do{ Client client = new Client(socket); if(client.getClientName() != null){ clients.addElement(client); if(checkName(client)){ //logger.log("信息-"+"目前有["+clients.size()+"]个用户已连接"); sendClients(client); client.start(); notifyRoom(); }else{ client.send(new StringBuffer("TAKEN")); disconnect(client); } i++; } break; }while(i<clients.size()); }else{ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO 自动生成 catch 块 //logger.log("错误-"+e.toString()); } } } } } |
//=======================================; // Client.java //=======================================; /* * 创建日期2005-10-10 * * TODO 要更改此生成的文件的模板,请转至 * 窗口-首选项- Java -代码样式-代码模板 */ package com.klstudio.socket.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; //import com.klstudio.util.Logger; /** * @author kinglong * * TODO 要更改此生成的类型注释的模板,请转至窗口-首选项- Java -代码样式-代码模板 */ public class Client extends Thread { private Socket clientSocket; private String clientName; private String clientIp; private BufferedReader br; private PrintStream ps; //private Logger logger; private ChatServer server; public Client(Socket socket) { //this.logger = new Logger(System.out); this.clientSocket = socket; try { this.br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8")); this.ps = new PrintStream(socket.getOutputStream(),true,"utf-8"); String info = this.br.readLine(); if(info!=null){ String[] info_arr = info.split(ChatServer.CommandDelimiters); if(info_arr.length>1){ this.clientName = info_arr[1]; } this.clientIp = socket.getRemoteSocketAddress().toString(); }else{ socket.close(); } } catch (IOException e) { // TODO 自动生成 catch 块 //this.logger.log("错误-" + e.toString()); } } /** * @return 返回 ip。 */ public String getClientIp() { return clientIp; } /** * @return 返回 name。 */ public String getClientName() { return clientName; } /** * @return 返回 socket。 */ public Socket getClientSocket() { return clientSocket; } public void send(StringBuffer msg){ this.ps.println(msg.toString()+"\0"); //this.ps.flush(); } public void run() { while (true) { String line = null; try { line = this.br.readLine(); } catch (IOException e) { // TODO 自动生成 catch 块 //this.logger.log("错误-" + e.toString()); ChatServer.disconnect(this); ChatServer.notifyRoom(); return; } if (line == null) { //this.logger.log("信息-[" + this.clientName + this.clientIp + "]用户离开!"); ChatServer.disconnect(this); ChatServer.notifyRoom(); if(this.clientName != null){ ChatServer.sendClients(new StringBuffer("MSG"+ChatServer.CommandDelimiters+"系统信息>[" + this.clientName + "]用户离开!")); } return; } //this.logger.log("信息-"+line); String[] cmd_arr = line.split(ChatServer.CommandDelimiters); String keyword = cmd_arr[0]; keyword = keyword.substring(1); if(keyword.equals("MSG")){ StringBuffer msg = new StringBuffer("MSG"+ChatServer.CommandDelimiters); msg.append(this.clientName+">"); msg.append(cmd_arr[1]); ChatServer.sendClients(msg,this.clientName,cmd_arr[2]); }else if(keyword.equals("QUIT")){ //this.logger.log("信息-[" + this.clientName + this.clientIp + "]用户离开!"); ChatServer.disconnect(this); ChatServer.notifyRoom(); ChatServer.sendClients(new StringBuffer("MSG"+ChatServer.CommandDelimiters+"系统信息>[" + this.clientName + "]用户离开!")); this.stop(); return; } } } } |
//注意,服务端向客户端发送的信息,必需以”\0”,空字符结尾,否则客户端也无法接收到信息!