Skip to content

Commit b6d1bbe

Browse files
committed
重写
1 parent 13b80c6 commit b6d1bbe

3 files changed

Lines changed: 31 additions & 26 deletions

File tree

src/main/java/cn/jja8/newbinggogo/NewBingGoGoClientWebSocket.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
package cn.jja8.newbinggogo;
22

33
import fi.iki.elonen.NanoWSD;
4+
import org.java_websocket.WebSocket;
45
import org.java_websocket.client.WebSocketClient;
6+
import org.java_websocket.framing.Framedata;
57
import org.java_websocket.handshake.ServerHandshake;
68

79
import java.io.IOException;
810
import java.net.URI;
11+
import java.util.LinkedList;
912

1013
public class NewBingGoGoClientWebSocket extends WebSocketClient {
1114
NewBingGoGoServerWebSocket newBingGoGoServerWebSocket;
12-
public NewBingGoGoClientWebSocket(URI serverUri,NewBingGoGoServerWebSocket newBingGoGoServerWebSocket) {
15+
LinkedList<String> messList;
16+
public NewBingGoGoClientWebSocket(URI serverUri,NewBingGoGoServerWebSocket newBingGoGoServerWebSocket,LinkedList<String> messList) {
1317
super(serverUri);
1418
this.newBingGoGoServerWebSocket = newBingGoGoServerWebSocket;
19+
this.messList = messList;
1520
}
1621

1722
@Override
1823
public void onOpen(ServerHandshake handshakedata) {
24+
for (String s : messList) {
25+
send(s);
26+
}
27+
}
28+
29+
@Override
30+
public void onWebsocketPong(WebSocket conn, Framedata f) {
31+
try {
32+
newBingGoGoServerWebSocket.ping(new byte[0]);
33+
} catch (IOException e) {
34+
close();
35+
}
1936

2037
}
2138

src/main/java/cn/jja8/newbinggogo/NewBingGoGoServer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
import java.net.URL;
1111
import java.util.HashMap;
1212
import java.util.Map;
13-
import java.util.concurrent.Executors;
14-
import java.util.concurrent.ScheduledExecutorService;
1513

1614
public class NewBingGoGoServer extends NanoWSD {
17-
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
18-
public static void main(String[] args) throws IOException {
15+
public static void main(String[] args) {
1916
if(args.length<1){
2017
System.err.print("需要指定运行端口号!");
2118
return;
@@ -70,7 +67,7 @@ public Response serve(IHTTPSession session) {
7067

7168
@Override
7269
protected WebSocket openWebSocket(IHTTPSession handshake) {
73-
return new NewBingGoGoServerWebSocket(handshake,executor);
70+
return new NewBingGoGoServerWebSocket(handshake);
7471
}
7572

7673
/*

src/main/java/cn/jja8/newbinggogo/NewBingGoGoServerWebSocket.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,49 @@
66
import java.io.IOException;
77
import java.net.URI;
88
import java.net.URISyntaxException;
9-
import java.util.concurrent.ScheduledExecutorService;
10-
import java.util.concurrent.ScheduledFuture;
11-
import java.util.concurrent.TimeUnit;
9+
import java.util.LinkedList;
1210

1311
public class NewBingGoGoServerWebSocket extends NanoWSD.WebSocket {
1412
NewBingGoGoClientWebSocket newBingGoGoClientWebSocket;
15-
ScheduledFuture<?> task;
16-
public NewBingGoGoServerWebSocket(NanoHTTPD.IHTTPSession handshakeRequest, ScheduledExecutorService executor) {
13+
LinkedList<String> messList = new LinkedList<>();
14+
public NewBingGoGoServerWebSocket(NanoHTTPD.IHTTPSession handshakeRequest) {
1715
super(handshakeRequest);
18-
task = executor.scheduleAtFixedRate(() -> {
19-
try {
20-
ping(new byte[1]);
21-
} catch (IOException e) {
22-
task.cancel(false);
23-
}
24-
}, 2, 2, TimeUnit.SECONDS);
25-
2616
URI url;
2717
try {
2818
url = new URI("wss://sydney.bing.com/sydney/ChatHub");
2919
} catch (URISyntaxException e) {
3020
throw new RuntimeException(e);//这个异常这辈子都不会出的
3121
}
3222

33-
newBingGoGoClientWebSocket = new NewBingGoGoClientWebSocket(url,this);
34-
newBingGoGoClientWebSocket.connect();
23+
newBingGoGoClientWebSocket = new NewBingGoGoClientWebSocket(url,this,messList);
3524
}
3625

3726
@Override
3827
protected void onOpen() {
39-
28+
newBingGoGoClientWebSocket.connect();
4029
}
4130

4231
@Override
4332
protected void onClose(NanoWSD.WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) {
44-
task.cancel(false);
4533
newBingGoGoClientWebSocket.close();
4634
}
4735

4836
@Override
4937
protected void onMessage(NanoWSD.WebSocketFrame message) {
50-
newBingGoGoClientWebSocket.send(message.getTextPayload());
38+
if(newBingGoGoClientWebSocket.isOpen()){
39+
newBingGoGoClientWebSocket.send(message.getTextPayload());
40+
}else {
41+
messList.addLast(message.getTextPayload());
42+
}
5143
}
5244

5345
@Override
5446
protected void onPong(NanoWSD.WebSocketFrame pong) {
55-
47+
newBingGoGoClientWebSocket.sendPing();
5648
}
5749

5850
@Override
5951
protected void onException(IOException exception) {
60-
task.cancel(false);
6152
newBingGoGoClientWebSocket.close();
6253
}
6354
}

0 commit comments

Comments
 (0)