这一节了解一下WebSocket的用法,WebSocket是一种基于TCP的全双工通信协议,通过它可以在客户端和服务器之间建立一个持久的连接,实现实时的双向数据传输。它的主要功能特点是允许服务器主动向客户端推送信息,同时也允许客户端主动向服务器发送信息。用法如下:
app下的gradle:
implementation"org.java-websocket:Java-WebSocket:1.5.3"
服务端:
import android.util.Log; import com.foxek.usb_custom_hid_demo.UsbPlay; 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; public class DCWebSocketServer extends WebSocketServer { public static String TAG ="DCWebSocketServer"; private Map
userMap = new HashMap(); public DCWebSocketServer(int port) { super(new InetSocketAddress(port)); } public void sendMessageToUser(String message) { message = message; if (userMap == null || userMap.isEmpty()) return; Set ketSet = 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 res = getClientInfo(conn); } @Override protected boolean onConnect(SelectionKey key) { return super.onConnect(key); } @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { String userHost = conn.getRemoteSocketAddress().getHostString(); if (userMap.containsKey(userHost)) { userMap.remove(userHost); } } @Override public void onMessage(WebSocket conn, String message) { if(message != null && message.contains("ScanCode")) { // sendMessageToUser(message); } } @Override public void onError(WebSocket conn, Exception ex) { } @Override public void onStart() { } private String getClientInfo(WebSocket conn) { return conn.getRemoteSocketAddress().getHostString() +":"+ conn.getRemoteSocketAddress().getPort(); } } import android.util.Log; import org.java_websocket.WebSocket; import java.util.HashMap; import java.util.Map; public class ServerSocketManager { public static String TAG ="ServerSocketManager"; private final int SOCKET_PORT = 8888; private DCWebSocketServer dcWebSocketServer; private Map userMap = new HashMap(); public ServerSocketManager() { this.dcWebSocketServer = new DCWebSocketServer(SOCKET_PORT); } private static class ServerSocketManagerHolder { private static final ServerSocketManager INSTANCE = new ServerSocketManager(); } public static ServerSocketManager getInstance() { return ServerSocketManagerHolder.INSTANCE; } public void sendMessageToAll(String message) { dcWebSocketServer.sendMessageToUser(message); } public boolean start() { try { dcWebSocketServer.start(); dcWebSocketServer.setReuseAddr(true); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public boolean stop() { try { dcWebSocketServer.stop(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } } 客户端:
import android.util.Log; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import java.net.URI; public class DCSocketManagerClient extends WebSocketClient { public static String TAG ="DCSocketManagerClien"; private static volatile DCSocketManagerClient client = null; public static DCSocketManagerClient getInstance(URI uri) { if (client == null) { synchronized (DCSocketManagerClient.class) { if (client == null) { client = new DCSocketManagerClient(uri); } } } return client; } public DCSocketManagerClient(URI serverUri) { super(serverUri); } @Override public void onOpen(ServerHandshake handshakedata) { } @Override public void onMessage(String message) { } @Override public void onClose(int code, String reason, boolean remote) { } @Override public void onError(Exception ex) { } } 启动服务端:
serverSocketManager = ServerSocketManager.getInstance() serverSocketManager!!.start() 启动客户端:
val uri = URI.create("ws://127.0.0.1:8888") clientSocketManager = DCSocketManagerClient.getInstance(uri) try { clientSocketManager?.connectBlocking() } catch (e: Exception) { e.printStackTrace() } 客户端发送消息:
if (clientSocketManager != null) { clientSocketManager!!.send("Java Coffee") } 服务端发送消息:
if (serverSocketManager != null) { serverSocketManager!!.sendMessageToAll("Drink Coffee From ServerMsg") }