java socketio server 端 笔记ITeye - 超凡娱乐

java socketio server 端 笔记ITeye

2019-01-12 19:00:41 | 作者: 天薇 | 标签: 客户端,装备,假如 | 浏览: 2443

import com.corundumstudio.socketio.AckRequest; import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIOServer; import com.corundumstudio.socketio.listener.ConnectListener; import com.corundumstudio.socketio.listener.DataListener; import com.corundumstudio.socketio.listener.DisconnectListener; import com.wafer.cloud.task.model.RegInfo; import com.wafer.cloud.task.model.SocketDataObj; * @author Chunf * @Time 2015年 public class SocketioServer implements Runnable { //静态音讯列表,SocketDataObj实体类 userid,message getter setter private static LinkedList SocketDataObj msgList = new LinkedList SocketDataObj //server装备容器 private static Configuration conf = null; //server已装备标识 private static boolean conf_flag = false; //server已发动标识 private static boolean server_flag = false; //server private static SocketIOServer server = null; //客户端暂存 private static HashMap String, SocketIOClient client_cache = new HashMap String, SocketIOClient //用户客户端联系暂存 private static HashMap UUID,String user_client_cache = new HashMap UUID,String //结构器 public SocketioServer(List SocketDataObj list) { if (!list.isEmpty()) { synchronized (msgList) { for (SocketDataObj s : list) { SocketioServer.msgList.add(s); msgList.notify(); //当有新的数据放入时,开释锁 //线程执行器 @Override public void run() { // TODO Auto-generated method stub while (true) {//翻开线程 synchronized (msgList) { //获取锁 if (!server_flag) { this.getConfig(); server = new SocketIOServer(conf); this.startServer(); if (!msgList.isEmpty()) { //假如音讯列表有数据,则需求推送 SocketDataObj sdo = msgList.getFirst(); String userid = sdo.getUserid(); SocketIOClient client = client_cache.get(userid); if (client != null) { client.sendEvent("msg", sdo.getMessage()); msgList.removeFirst(); } else { try { msgList.wait(); //假如音讯列表无数据则wait 坚持线程 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); //生成装备容器 private void getConfig() { if (!conf_flag) { conf = new Configuration(); try { String hostName = PropertiesReader.getValByKey("socketioIp"); String port = PropertiesReader.getValByKey("socketPort"); if (hostName != null !StringUtils.isEmpty(hostName) port != null !StringUtils.isEmpty(port)) { conf.setHostname(hostName); conf.setPort(Integer.parseInt(port)); conf_flag = true; } catch (Exception e) { try { throw new Exception("获取装备信息犯错!"); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); //发动server添加监听器 private void startServer() { if (!server_flag) { this.getConfig(); server.start(); server_flag = true; server.addConnectListener(new ConnectListener() { @Override public void onConnect(SocketIOClient client) { // TODO Auto-generated method stub server.addDisconnectListener(new DisconnectListener() { @Override public void onDisconnect(SocketIOClient client) { // TODO Auto-generated method stub //依据客户端sessionID获取用户与client缓存中的信息 String userid = user_client_cache.get(client.getSessionId()); if(userid!=null){ if (client_cache.get(userid).getSessionId().equals(client.getSessionId())){ //假如当时缓存中的client就是断开的client //铲除当时信息 client_cache.remove(userid); //铲除联系缓存中的信息 user_client_cache.remove(client.getSessionId()); //添加regId监听,regId是与前台监听名对应共同的。当时台客户端连接上server后,向后台发送一个regId值,后台将这个值作为该客户端的仅有标识,并与client绑定存储 //regInfo实体类中只要一个特点就是userid + getter setter server.addEventListener("regId", RegInfo.class, new DataListener RegInfo () { //完成dataListener的回调 @Override public void onData(SocketIOClient client, RegInfo data, AckRequest ackSender) throws Exception { // TODO Auto-generated method stub //userid即为前台传到后台的regId String userid = data.getUserID(); if (userid != null StringUtils.isNotEmpty(userid)) { //因为一个客户端是外部轮询的,所以一直在改变 //在客户端缓存中铲除原有归于该userid的客户端 client_cache.remove(userid); //添加新的客户端 client_cache.put(userid, client); //向用户与客户端存储中存入新的client user_client_cache.put(client.getSessionId(), userid); client.sendEvent("regOK", data); System.out.println("注册成功!key=" + userid); @SuppressWarnings("unused") private void stopServer() { if (server_flag) { server.stop(); server_flag = false;
假如用cache作为暂存 作用更佳!
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表超凡娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章