Skip to content

Commit ca7aabd

Browse files
committed
unifying
1 parent a80b0d7 commit ca7aabd

26 files changed

+264
-888
lines changed

hello-websocket-flutter-client/lib/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class _MyAppState extends State<MyApp> {
1414
// Localhost for android - 10.0.2.2
1515
// Localhost for iOS - 127.0.0.1
1616
final IOWebSocketChannel channel =
17-
IOWebSocketChannel.connect('ws://127.0.0.1:2088');
17+
IOWebSocketChannel.connect('ws://127.0.0.1:9898');
1818

1919
var userInput;
2020
List messages = [];

hello-websocket-go/client/echo_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131

3232
input := make(chan string, 1)
3333
go getInput(input)
34-
host := "localhost:8080"
34+
host := "localhost:9898"
3535
URL := url.URL{Scheme: "ws", Host: host}
3636
firstArg := os.Args[1]
3737
header := http.Header{}

hello-websocket-go/server/echo_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434
fmt.Printf("Websocket Connected(%s:%s) X=%s\n", clientAddr, clientPort, name)
3535
listen(websocket)
3636
})
37-
http.ListenAndServe(":8080", nil)
37+
http.ListenAndServe(":9898", nil)
3838
}
3939

4040
func listen(conn *websocket.Conn) {

hello-websocket-java/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# hello websocket java(netty)
22

3+
[Java API for WebSocket JSR 356](https://jcp.org/en/jsr/detail?id=356)
4+
35
## build & run server
46

57
```sh

hello-websocket-java/client_build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
2+
set -e
23
export DEPLOY_NAME=hello.websocket.java.client
3-
export MAIN_CLASS=org.feuyeux.websocket.EchoClient
4-
#export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
4+
export MAIN_CLASS=org.feuyeux.websocket.HelloClient
55
mvn clean package
66

77
if [ ! -d "bin" ]; then

hello-websocket-java/client_run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
2-
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
2+
#export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
33
java -jar bin/client.jar

hello-websocket-java/pom.xml

+25-11
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
<properties>
1212
<maven.compiler.source>21</maven.compiler.source>
1313
<maven.compiler.target>21</maven.compiler.target>
14+
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
1415
<log4j.version>2.20.0</log4j.version>
16+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
1517
<lombok.version>1.18.30</lombok.version>
16-
<netty.version>4.1.89.Final</netty.version>
18+
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
19+
<netty.version>4.1.100.Final</netty.version>
20+
<!-- https://mvnrepository.com/artifact/com.coveo/fmt-maven-plugin -->
21+
<fmt-maven-plugin.version>2.21.1</fmt-maven-plugin.version>
1722
</properties>
1823

1924
<dependencies>
@@ -23,21 +28,13 @@
2328
<version>${netty.version}</version>
2429
</dependency>
2530

31+
<!-- SLF4J Bridge slf4j to log4j2 -->
32+
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
2633
<dependency>
2734
<groupId>org.apache.logging.log4j</groupId>
2835
<artifactId>log4j-slf4j-impl</artifactId>
2936
<version>${log4j.version}</version>
3037
</dependency>
31-
<dependency>
32-
<groupId>org.apache.logging.log4j</groupId>
33-
<artifactId>log4j-api</artifactId>
34-
<version>${log4j.version}</version>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.apache.logging.log4j</groupId>
38-
<artifactId>log4j-core</artifactId>
39-
<version>${log4j.version}</version>
40-
</dependency>
4138

4239
<dependency>
4340
<groupId>org.projectlombok</groupId>
@@ -74,6 +71,23 @@
7471
</execution>
7572
</executions>
7673
</plugin>
74+
<plugin>
75+
<groupId>com.spotify.fmt</groupId>
76+
<artifactId>fmt-maven-plugin</artifactId>
77+
<version>${fmt-maven-plugin.version}</version>
78+
<configuration>
79+
<skipTestSourceDirectory>true</skipTestSourceDirectory>
80+
<skipSortingImports>false</skipSortingImports>
81+
<style>google</style>
82+
</configuration>
83+
<executions>
84+
<execution>
85+
<goals>
86+
<goal>format</goal>
87+
</goals>
88+
</execution>
89+
</executions>
90+
</plugin>
7791
</plugins>
7892
</build>
7993
</project>

hello-websocket-java/server_build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
2+
set -e
23
export DEPLOY_NAME=hello.websocket.java.server
3-
export MAIN_CLASS=org.feuyeux.websocket.EchoServer
4-
# export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
4+
export MAIN_CLASS=org.feuyeux.websocket.HelloServer
55
mvn clean package
66

77
if [ ! -d "bin" ]; then

hello-websocket-java/server_run.sh

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/usr/bin/env bash
2-
#export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
32
java -jar bin/server.jar

hello-websocket-java/src/main/java/org/feuyeux/websocket/EchoClient.java

-93
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.feuyeux.websocket;
2+
3+
import static org.feuyeux.websocket.config.EchoConfig.*;
4+
5+
import io.netty.bootstrap.Bootstrap;
6+
import io.netty.buffer.ByteBuf;
7+
import io.netty.channel.Channel;
8+
import io.netty.channel.EventLoopGroup;
9+
import io.netty.channel.nio.NioEventLoopGroup;
10+
import io.netty.channel.socket.nio.NioSocketChannel;
11+
import io.netty.handler.codec.http.DefaultHttpHeaders;
12+
import io.netty.handler.codec.http.websocketx.*;
13+
import java.net.URI;
14+
import lombok.extern.slf4j.Slf4j;
15+
import org.feuyeux.websocket.client.WebSocketClientHandler;
16+
import org.feuyeux.websocket.client.WebSocketClientInitializer;
17+
import org.feuyeux.websocket.codec.EchoRequestCodec;
18+
import org.feuyeux.websocket.info.EchoRequest;
19+
20+
/**
21+
* Echo Client <a
22+
* href="https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java">Sample</a>
23+
*/
24+
@Slf4j
25+
public class HelloClient {
26+
27+
public Channel open(EventLoopGroup group) {
28+
String URL;
29+
if (SSL) {
30+
URL = String.format("wss://%s:9899%s", host, WEBSOCKET_PATH);
31+
} else {
32+
URL = String.format("ws://%s:9898%s", host, WEBSOCKET_PATH);
33+
}
34+
URI uri = URI.create(URL);
35+
// Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
36+
// If you change it to V00, ping is not supported and remember to change
37+
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
38+
WebSocketClientHandshaker handShaker =
39+
WebSocketClientHandshakerFactory.newHandshaker(
40+
uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders());
41+
final WebSocketClientHandler clientHandler = new WebSocketClientHandler(handShaker);
42+
final WebSocketClientInitializer clientInitializer =
43+
new WebSocketClientInitializer(clientHandler);
44+
45+
Bootstrap b = new Bootstrap();
46+
b.group(group).channel(NioSocketChannel.class).handler(clientInitializer);
47+
try {
48+
log.info("Connecting to server({})", URL);
49+
Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel();
50+
clientHandler.handshakeFuture().sync();
51+
return ch;
52+
} catch (Exception e) {
53+
log.error("", e);
54+
}
55+
return null;
56+
}
57+
58+
public void close(Channel ch) throws InterruptedException {
59+
log.info("Closing the connection");
60+
ch.writeAndFlush(new CloseWebSocketFrame());
61+
ch.closeFuture().sync();
62+
}
63+
64+
public void send(Channel ch) {
65+
log.info("Sending messages");
66+
sendBinary(ch, new EchoRequest(81, "わかった"));
67+
sendBinary(ch, new EchoRequest(82, "알았어"));
68+
sendBinary(ch, new EchoRequest(44, "Got it"));
69+
sendBinary(ch, new EchoRequest(33, "Je l'ai"));
70+
sendBinary(ch, new EchoRequest(7, "Понял"));
71+
sendBinary(ch, new EchoRequest(30, "Το έπιασα"));
72+
sendText(ch, "ru:Большое спасибо");
73+
sendText(ch, "fr:Merci beaucoup");
74+
sendText(ch, "es:Muchas Gracias");
75+
sendText(ch, "ar:" + "شكرا جزيلا");
76+
sendText(ch, "he:" + "תודה רבה");
77+
log.info("Complete to send");
78+
}
79+
80+
public void sendText(Channel ch, final String text) {
81+
TextWebSocketFrame textFrame = new TextWebSocketFrame(text);
82+
ch.writeAndFlush(textFrame);
83+
}
84+
85+
public void sendBinary(Channel ch, final EchoRequest echoRequest) {
86+
ByteBuf byteBuf = EchoRequestCodec.encode(echoRequest);
87+
BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame(byteBuf);
88+
ch.writeAndFlush(binaryFrame);
89+
}
90+
91+
public static void main(String[] args) throws Exception {
92+
EventLoopGroup group = new NioEventLoopGroup();
93+
final HelloClient client = new HelloClient();
94+
Channel ch = client.open(group);
95+
if (ch != null) {
96+
client.send(ch);
97+
client.close(ch);
98+
}
99+
group.shutdownGracefully();
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
package org.feuyeux.websocket;
22

3-
import static org.feuyeux.websocket.config.EchoConfig.host;
4-
import static org.feuyeux.websocket.config.EchoConfig.port;
3+
import static org.feuyeux.websocket.config.EchoConfig.*;
54

65
import io.netty.bootstrap.ServerBootstrap;
76
import io.netty.channel.Channel;
87
import io.netty.channel.ChannelFuture;
98
import io.netty.channel.EventLoopGroup;
109
import io.netty.channel.nio.NioEventLoopGroup;
1110
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
import io.netty.handler.logging.LogLevel;
12+
import io.netty.handler.logging.LoggingHandler;
1213
import java.time.LocalDateTime;
1314
import lombok.extern.slf4j.Slf4j;
1415
import org.feuyeux.websocket.server.WebSocketServerInitializer;
1516

1617
@Slf4j
17-
public class EchoServer {
18-
19-
private Channel ch;
20-
18+
public class HelloServer {
2119
public void run() throws Exception {
20+
final int port = Integer.parseInt(System.getProperty("port", SSL ? "9899" : "9898"));
21+
2222
final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
2323
final EventLoopGroup workerGroup = new NioEventLoopGroup(2);
2424
try {
2525
final ServerBootstrap b = new ServerBootstrap();
2626
b.group(bossGroup, workerGroup)
2727
.channel(NioServerSocketChannel.class)
28+
.handler(new LoggingHandler(LogLevel.INFO))
2829
.childHandler(new WebSocketServerInitializer());
29-
ChannelFuture bind = b.bind(host, port);
30-
ch = bind.addListener(f -> log.info("EchoServer start({}:{})", host, port)).sync().channel();
31-
ch.closeFuture().sync();
30+
ChannelFuture bind = b.bind(port);
31+
Channel ch = bind.addListener(f -> log.info("EchoServer start(:{})", port)).sync().channel();
32+
ch.closeFuture().syncUninterruptibly();
3233
} finally {
3334
bossGroup.shutdownGracefully();
3435
workerGroup.shutdownGracefully();
@@ -39,6 +40,6 @@ public void run() throws Exception {
3940
public static void main(final String[] args) throws Exception {
4041
LocalDateTime now = LocalDateTime.now();
4142
log.info("{}", now);
42-
new EchoServer().run();
43+
new HelloServer().run();
4344
}
4445
}

0 commit comments

Comments
 (0)