import org.greenrobot.eventbus.EventBus; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; import java.net.InetSocketAddress; import java.nio.channels.SelectionKey; import java.util.HashMap; import java.util.Map; import java.util.Set; import mwteck.ettda.util.L; /** * CreateTime 2019/6/6 10:04 * Author LiuShiHua * Description: */ public class MyServerSocket extends WebSocketServer { private EventBus bus = null; private MapuserMap = new HashMap();//存储客户端信息 public MyServerSocket(int port) {//默认端口号是80,这里自定义了自己的端口号 super(new InetSocketAddress(port)); bus = EventBus.getDefault(); } //发送消息给所有用户 public void sendMessageToUser(String message) { message = "服务端说:" + message; bus.post(message); if (userMap == null || userMap.isEmpty()) return; SetketSet = userMap.keySet(); for (String key : ketSet) { WebSocket clientSocket = userMap.get(key); if (clientSocket != null) { clientSocket.send(message); } } } @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { String userHost = conn.getRemoteSocketAddress().getHostString(); if (!userMap.containsKey(userHost)) {//判断 userMap.put(userHost, conn); } String openStr = "onOpen客户端 " + getClientInfo(conn) + " 上线"; bus.post(openStr); L.d(openStr); } //客户端离开或者服务端关闭时调用 @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { String userHost = conn.getRemoteSocketAddress().getHostString(); if (userMap.containsKey(userHost)) { userMap.remove(userHost); } String closeStr = ""; if (remote) {//远程关闭连接 closeStr = "onClose客户端关闭:code = " + code + ":reason = " + reason + ":remote = " + remote; } else {//服务端关闭连接 closeStr = "onClose本地关闭:code = " + code + ":reason = " + reason + ":remote = " + remote; } bus.post(closeStr); L.d(closeStr); } //收到消息时调用 @Override public void onMessage(WebSocket conn, String message) { String msg = getClientInfo(conn) + "说:" + message; bus.post(msg); L.d("onMessage " + msg); } @Override public void onError(WebSocket conn, Exception ex) { String err = "onError " + getClientInfo(conn) + ":" + ex.getMessage(); bus.post(err); L.d(err); } //用户连接进来时先调用此方法在调用onOpen @Override protected boolean onConnect(SelectionKey key) { bus.post("onConnect"); L.d("onConnect:SelectionKey" + key.channel().toString()); return super.onConnect(key); } @Override public void onStart() { String con = "onStart:port = " + this.getAddress().getPort(); bus.post(con); L.d(con); } //获取客户端host:port private String getClientInfo(WebSocket conn) { return conn.getRemoteSocketAddress().getHostString() + ":" + conn.getRemoteSocketAddress().getPort(); } }/** * CreateTime 2019/6/5 13:55 * Author LiuShiHua * Description: */ import android.util.Log; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import mwteck.ettda.util.L; /** * Created by Roc on 2018/10/9. */ public class ServerSocketManager { private MyServerSocket serverSocket = null; private final int SOCKET_PORT = 8201; private MapuserMap = new HashMap(); public ServerSocketManager() { this.serverSocket = new MyServerSocket(SOCKET_PORT); } //将消息发送给所有用户 public void sendMessageToAll(String message) { serverSocket.sendMessageToUser(message); } //开启服务 public boolean start() { try { serverSocket.start(); L.d("serverSocket is start"); return true; } catch (Exception e) { L.d("serverSocket start onError:" + e.getMessage()); e.printStackTrace(); return false; } } public boolean stop() { try { serverSocket.stop(); return true; } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } return false; } }import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketFactory; import com.neovisionaries.ws.client.WebSocketFrame; import org.greenrobot.eventbus.EventBus; import java.io.IOException; import java.util.List; import java.util.Map; import mwteck.ettda.util.L; /** * CreateTime 2019/6/4 15:12 * Author LiuShiHua * Description: */ public class ClientSocketManager { private final int FRAME_QUEUE_SIZE = 5; private final int CONNECT_TIMEOUT = 120 * 1000; private WebSocket clientSocket; private EventBus bus = null; private String socketUrl; private WebSocketAdapter adapter = new WebSocketAdapter() { @Override public void onTextMessage(WebSocket websocket, String text) throws Exception { super.onTextMessage(websocket, text); String[] msgs = text.split("\\|"); if (msgs.length >= 2) { for (int i = 0; i