一、pom依赖
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.1</version>
</dependency>
二、服务端代码
package com.peck.blockchain.p2p;
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 org.testng.util.Strings;
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);
private int port = 7001;
private List<WebSocket> localSockets = new ArrayList<WebSocket>();
public List<WebSocket> getLocalSockets() {
return localSockets;
}
public void setLocalSockets(List<WebSocket> localSockets) {
this.localSockets = localSockets;
}
@PostConstruct
@Order(1)
public void initServer() {
final WebSocketServer socketServer = new WebSocketServer(new InetSocketAddress(port)) {
@Override
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
sendMessage(webSocket,"北京服务端成功创建连接");
localSockets.add(webSocket);
}
@Override
public void onClose(WebSocket webSocket, int i, String s, boolean b) {
logger.info(webSocket.getRemoteSocketAddress() + "客户端与服务器断开连接!");
localSockets.remove(webSocket);
}
@Override
public void onMessage(WebSocket webSocket, String msg) {
logger.info("北京服务端接收到客户端消息:" + msg);
sendMessage(webSocket,"收到消息");
}
@Override
public void onError(WebSocket webSocket, Exception e) {
logger.info(webSocket.getRemoteSocketAddress() + "客户端连接错误!");
localSockets.remove(webSocket);
}
@Override
public void onStart() {
logger.info("北京服务端监听socketServer端启动......");
}
};
socketServer.start();
logger.info("北京服务端监听socketServer端口:" + port);
}
public void sendMessage(WebSocket ws,String message) {
logger.info("发送给" + ws.getRemoteSocketAddress().getPort() + "的篇p2p消息是:" + message);
ws.send(message);
}
public void broatcast(String message) {
if (localSockets.size() == 0 || Strings.isNullOrEmpty(message)) {
return;
}
logger.info("Glad to say broatcast to clients being startted!");
for (WebSocket socket : localSockets) {
this.sendMessage(socket,message);
}
logger.info("Glad to say broatcast to clients has overred!");
}
}
三、客户端代码
package com.peck.blockchain.p2p;
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 org.testng.util.Strings;
import javax.annotation.PostConstruct;
import java.net.URI;
import java.net.URISyntaxException;
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/";
private List<WebSocket> localSockets = new ArrayList<>();
public List<WebSocket> getLocalSockets() {
return localSockets;
}
public void setLocalSockets(List<WebSocket> localSockets) {
this.localSockets = localSockets;
}
@PostConstruct
@Order(2)
public void connentPeer() {
try {
final WebSocketClient socketClient = new WebSocketClient(new URI(wsUrl)) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
sendMessage(this,"北京客户端成功创建客户端");
localSockets.add(this);
}
@Override
public void onMessage(String msg) {
logger.info("北京客户端收到北京服务端发送的消息:" + msg);
}
@Override
public void onClose(int i, String s, boolean b) {
logger.info("北京客户端关闭");
localSockets.remove(this);
}
@Override
public void onError(Exception e) {
logger.info("北京客户端报错");
localSockets.remove(this);
}
};
socketClient.connect();
}catch (URISyntaxException e) {
e.printStackTrace();
}
}
public void sendMessage(WebSocket ws,String message) {
logger.info("发送给" + ws.getRemoteSocketAddress().getPort() + "的p2p消息:" + message);
ws.send(message);
}
public void broadcast(String message) {
if (localSockets.size() == 0 || Strings.isNullOrEmpty(message)) {
return;
}
logger.info("Glad to say broatcast to servers being startted!");
for (WebSocket socket:localSockets) {
this.sendMessage(socket,message);
}
logger.info("Glad to say broatcast to servers has overred!");
}
}
四、运行截图
|