3
3
import java .nio .channels .spi .SelectorProvider ;
4
4
import java .util .concurrent .ThreadFactory ;
5
5
6
+ import org .eclipse .jetty .websocket .jsr356 .server .deploy .WebSocketServerContainerInitializer ;
6
7
import org .nlpcn .jcoder .server .rpc .client .RpcDecoder ;
7
8
import org .nlpcn .jcoder .server .rpc .client .RpcEncoder ;
8
9
import org .nlpcn .jcoder .server .rpc .client .RpcRequest ;
21
22
import io .netty .channel .socket .nio .NioServerSocketChannel ;
22
23
import io .netty .handler .codec .LengthFieldBasedFrameDecoder ;
23
24
import io .netty .handler .codec .LengthFieldPrepender ;
25
+ import io .netty .handler .codec .http .HttpObjectAggregator ;
26
+ import io .netty .handler .codec .http .HttpServerCodec ;
27
+ import io .netty .handler .codec .http .websocketx .WebSocketServerProtocolHandler ;
24
28
25
29
/**
26
30
* rpc server
29
33
*
30
34
*/
31
35
public class RpcServer {
32
-
33
- private static final Logger LOG = LoggerFactory .getLogger (RpcServer .class ) ;
36
+
37
+ private static final Logger LOG = LoggerFactory .getLogger (RpcServer .class );
34
38
35
39
private static ServerBootstrap bootstrap = null ;
36
40
@@ -42,14 +46,16 @@ public class RpcServer {
42
46
43
47
private static final int MESSAGE_LENGTH = 4 ;
44
48
49
+ private static final String WEBSOCKET_PATH = "/" ;
50
+
45
51
/**
46
52
*
47
53
* @throws Exception
48
54
*/
49
55
public static void startServer (int port ) throws Exception {
50
-
56
+
51
57
LOG .info ("to start rpc server " );
52
-
58
+
53
59
ThreadFactory threadRpcFactory = new NamedThreadFactory ("NettyRPC ThreadFactory" );
54
60
55
61
boss = new NioEventLoopGroup (PARALLEL );
@@ -61,8 +67,8 @@ public static void startServer(int port) throws Exception {
61
67
@ Override
62
68
protected void initChannel (SocketChannel socketChannel ) throws Exception {
63
69
ChannelPipeline pipeline = socketChannel .pipeline ();
64
- pipeline .addLast (new LengthFieldBasedFrameDecoder ( Integer . MAX_VALUE , 0 , MESSAGE_LENGTH , 0 , MESSAGE_LENGTH ));
65
- pipeline .addLast (new LengthFieldPrepender ( MESSAGE_LENGTH ));
70
+ pipeline .addLast (new HttpServerCodec ( ));
71
+ pipeline .addLast (new WebSocketServerProtocolHandler ( WEBSOCKET_PATH , null , true ));
66
72
pipeline .addLast (new RpcDecoder (RpcRequest .class ));
67
73
pipeline .addLast (new RpcEncoder (RpcResponse .class ));
68
74
pipeline .addLast (new ContextHandler ());
@@ -73,11 +79,11 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {
73
79
ChannelFuture future = bootstrap .bind (port ).sync ();
74
80
75
81
future .channel ().closeFuture ();
76
-
82
+
77
83
LOG .info ("start rpc server ok" );
78
84
79
85
} finally {
80
-
86
+
81
87
}
82
88
}
83
89
0 commit comments