科讯网站模版网,做网站一定要公司备案吗,黄页网络的推广软件,制作公司网站要多少钱一、什么是websocket WebSocket是一种在Web应用程序中实现实时双向通信的协议。它为浏览器和服务器之间提供了一种持久连接#xff0c;在一个连接上可以双向传输数据。相比传统的HTTP协议#xff0c;WebSocket具有更低的延迟和更高的效率。 WebSocket使用了类似于握手的方式来…一、什么是websocket WebSocket是一种在Web应用程序中实现实时双向通信的协议。它为浏览器和服务器之间提供了一种持久连接在一个连接上可以双向传输数据。相比传统的HTTP协议WebSocket具有更低的延迟和更高的效率。 WebSocket使用了类似于握手的方式来建立连接。在握手过程中浏览器和服务器会交换一些信息以建立一个WebSocket连接。一旦连接建立浏览器和服务器之间就可以实时地传输数据而不需要每次都发起新的HTTP请求。 WebSocket协议可以在不同的平台和编程语言中实现包括Web浏览器和服务器端。在Web浏览器中可以使用JavaScript中的WebSocket API来实现WebSocket连接。在服务器端可以使用各种编程语言和框架来实现WebSocket服务器。 WebSocket的应用场景非常广泛。它可以用于实时聊天应用、实时游戏、实时股票行情、实时协作编辑等需要实时通信的应用程序。通过WebSocket开发人员可以更加方便地实现实时通信功能提高用户体验。 这里我们使用Java配合springboot2进行使用。
二、依赖坐标地址
1.springBoot父级依赖 !--依赖的父级工程--
parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.6/versionrelativePath/
/parent 2.springBoot依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency 3.webSocket依赖 dependencygroupIdorg.java-websocket/groupIdartifactIdJava-WebSocket/artifactIdversion1.3.8/version
/dependency 三、代码 不多说了直接拿去看吧都在注释里面了。
服务器
package com.blockchain.qgy.network.websocket;import com.blockchain.qgy.util.Strings;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;Component
public class P2pPointServer {//日志记录private Logger logger LoggerFactory.getLogger(P2pPointServer.class);//本机的Server的WebSocket端口private Integer port 7001;//所有连接到服务器的WebSocket缓存器private ListWebSocket localSockets new ArrayList();public ListWebSocket getLocalSockets() {return localSockets;}public void setLocalSockets(ListWebSocket localSockets) {this.localSockets localSockets;}PostConstructOrder(1)public void initServer(){final WebSocketServer socketServer new WebSocketServer(new InetSocketAddress(port)) {//创建链接成功时触发Overridepublic void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {sendMessage(webSocket,孩子们我回来了);localSockets.add(webSocket);}//断开连接时触发Overridepublic void onClose(WebSocket webSocket, int i, String s, boolean b) {logger.info(webSocket.getRemoteSocketAddress() 客户端与服务器断开连接);localSockets.remove(webSocket);}//接收到消息时触发Overridepublic void onMessage(WebSocket webSocket, String s) {logger.info(收到了客户端的消息 s);sendMessage(webSocket,孩子们我收到消息了);}//连接发送错误时调用后触发onClose方法Overridepublic void onError(WebSocket webSocket, Exception e) {logger.info(webSocket.getRemoteSocketAddress() 客户端与服务器连接发送错误);localSockets.remove(webSocket);}Overridepublic void onStart() {logger.info(孩子们我要启动了);}};socketServer.start();logger.info(Man!what can I say,我已经启动了);}/*** 一对一发送* param webSocket客户端* param message信息*/public void sendMessage(WebSocket webSocket,String message){logger.info(发送给webSocket.getRemoteSocketAddress().getPort() 的p2p消息是 message);webSocket.send(message);}/*** 一对多发送* param message信息*/public void broatcast(String message){if (localSockets.size() 0 || Strings.isNullOrEmpty(message)) return;logger.info(张开第三只眼);for (WebSocket webSocket : localSockets){this.sendMessage(webSocket,message);}logger.info(孩子们我燃尽了);}
}客户端
package com.blockchain.qgy.network.websocket;import com.blockchain.qgy.util.Strings;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;Component
public class P2pPointClient {//日志记录private Logger logger LoggerFactory.getLogger(P2pPointClient.class);//服务器端的地址private String wsUrl ws://localhost:7001/;//所有连接到客户端的WebSocket缓存器private ListWebSocket localSockets new ArrayList();public ListWebSocket getLocalSockets() {return localSockets;}public void setLocalSockets(ListWebSocket localSockets) {this.localSockets localSockets;}PostConstructOrder(2)public void connectServer(){try {final WebSocketClient socketClient new WebSocketClient(new URI(wsUrl)) {Overridepublic void onOpen(ServerHandshake serverHandshake) {sendMessage(this,孩子们我是客户端);localSockets.add(this);}Overridepublic void onMessage(String s) {logger.info(收到服务器发来的信息s);}Overridepublic void onClose(int i, String s, boolean b) {logger.info(断开连接);localSockets.remove(this);}Overridepublic void onError(Exception e) {logger.info(连接错误);localSockets.remove(this);}};socketClient.connect();} catch (Exception e) {e.printStackTrace();logger.info(连接错误);}}/*** 一对一发送* param webSocket客户端* param message信息*/public void sendMessage(WebSocket webSocket,String message){logger.info(发送给webSocket.getRemoteSocketAddress().getPort() 的p2p消息是 message);webSocket.send(message);}/*** 一对多发送* param message信息*/public void broatcast(String message){if (localSockets.size() 0 || Strings.isNullOrEmpty(message)) return;logger.info(张开第三只眼);for (WebSocket webSocket : localSockets){this.sendMessage(webSocket,message);}logger.info(孩子们我燃尽了);}
}