mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-18 21:26:35 +00:00
feat[net]: 增加websocket client,便于来调试websocket server
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.core.websocket;
|
||||
|
||||
import com.zfoo.net.core.AbstractClient;
|
||||
import com.zfoo.net.handler.ClientDispatcherHandler;
|
||||
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
|
||||
import com.zfoo.util.net.HostAndPort;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.http.HttpClientCodec;
|
||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
|
||||
import io.netty.handler.stream.ChunkedWriteHandler;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class WebsocketClient extends AbstractClient {
|
||||
|
||||
private WebSocketClientProtocolConfig webSocketClientProtocolConfig;
|
||||
|
||||
public WebsocketClient(HostAndPort host, WebSocketClientProtocolConfig webSocketClientProtocolConfig) {
|
||||
super(host);
|
||||
this.webSocketClientProtocolConfig = webSocketClientProtocolConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelInitializer<? extends Channel> channelChannelInitializer() {
|
||||
return new ChannelHandlerInitializer();
|
||||
}
|
||||
|
||||
|
||||
public class ChannelHandlerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
@Override
|
||||
public void initChannel(SocketChannel channel) {
|
||||
channel.pipeline().addLast(new HttpClientCodec());
|
||||
channel.pipeline().addLast(new ChunkedWriteHandler());
|
||||
channel.pipeline().addLast(new HttpObjectAggregator(64 * 1024));
|
||||
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
|
||||
channel.pipeline().addLast(new WebSocketCodecHandler());
|
||||
channel.pipeline().addLast(new ClientDispatcherHandler());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+7
-10
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
@@ -10,11 +11,10 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.core.websocket.server;
|
||||
package com.zfoo.net.core.websocket.client;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.dispatcher.model.anno.PacketReceiver;
|
||||
import com.zfoo.net.packet.websocket.CM_WebSocketPacket;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloResponse;
|
||||
import com.zfoo.net.session.model.Session;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -26,16 +26,13 @@ import org.springframework.stereotype.Component;
|
||||
* @version 3.0
|
||||
*/
|
||||
@Component
|
||||
public class WebsocketPacketController {
|
||||
public class WebsocketClientPacketController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketPacketController.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketClientPacketController.class);
|
||||
|
||||
@PacketReceiver
|
||||
public void atCM_WebSocketPacket(Session session, CM_WebSocketPacket cm) {
|
||||
logger.info("websocket server receive [packet:{}] from browser", JsonUtils.object2String(cm));
|
||||
|
||||
|
||||
NetContext.getDispatcher().send(session, cm);
|
||||
public void atWebsocketHelloResponse(Session session, WebsocketHelloResponse response) {
|
||||
logger.info("websocket client receive [packet:{}] from server", JsonUtils.object2String(response));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.core.websocket.client;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.core.websocket.WebsocketClient;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloRequest;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import com.zfoo.util.net.HostAndPort;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class WebsocketClientTest {
|
||||
|
||||
@Test
|
||||
public void startClient() {
|
||||
var context = new ClassPathXmlApplicationContext("config.xml");
|
||||
|
||||
var webSocketClientProtocolConfig = WebSocketClientProtocolConfig.newBuilder()
|
||||
.webSocketUri("http://127.0.0.1:9000/websocket")
|
||||
.build();
|
||||
|
||||
var client = new WebsocketClient(HostAndPort.valueOf("127.0.0.1:9000"), webSocketClientProtocolConfig);
|
||||
var session = client.start();
|
||||
|
||||
var request = new WebsocketHelloRequest();
|
||||
request.setMessage("Hello, this is the websocket client!");
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ThreadUtils.sleep(2000);
|
||||
NetContext.getDispatcher().send(session, request);
|
||||
}
|
||||
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
<!--
|
||||
~ Copyright (C) 2020 The zfoo Authors
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
~ in compliance with the License. You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>websocket client test</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>请使用zweb测试</h1>
|
||||
</body>
|
||||
</html>
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.core.websocket.server;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.dispatcher.model.anno.PacketReceiver;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloRequest;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloResponse;
|
||||
import com.zfoo.net.session.model.Session;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
@Component
|
||||
public class WebsocketServerPacketController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketServerPacketController.class);
|
||||
|
||||
@PacketReceiver
|
||||
public void atWebsocketHelloRequest(Session session, WebsocketHelloRequest request) {
|
||||
logger.info("receive [packet:{}] from browser", JsonUtils.object2String(request));
|
||||
|
||||
var response = new WebsocketHelloResponse();
|
||||
response.setMessage("Hello, this is the websocket server!");
|
||||
|
||||
NetContext.getDispatcher().send(session, response);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
package com.zfoo.net.core.websocket.server;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.core.websocket.WebsocketServer;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import com.zfoo.util.net.HostAndPort;
|
||||
@@ -32,7 +31,7 @@ public class WebsocketServerTest {
|
||||
public void startServer() {
|
||||
var context = new ClassPathXmlApplicationContext("config.xml");
|
||||
|
||||
var server = new WebsocketServer(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
||||
var server = new WebsocketServer(HostAndPort.valueOf("127.0.0.1:9000"));
|
||||
server.start();
|
||||
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ import java.util.Set;
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class CM_WebSocketPacket implements IPacket {
|
||||
public class WebSocketPacketRequest implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 2070;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.packet.websocket;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class WebsocketHelloRequest implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 1400;
|
||||
|
||||
private String message;
|
||||
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
return PROTOCOL_ID;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.packet.websocket;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class WebsocketHelloResponse implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 1401;
|
||||
|
||||
private String message;
|
||||
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
return PROTOCOL_ID;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -65,10 +65,13 @@
|
||||
|
||||
<protocol id="1300" location="com.zfoo.net.packet.tcp.TcpHelloRequest"/>
|
||||
<protocol id="1301" location="com.zfoo.net.packet.tcp.TcpHelloResponse"/>
|
||||
|
||||
<protocol id="1400" location="com.zfoo.net.packet.websocket.WebsocketHelloRequest"/>
|
||||
<protocol id="1401" location="com.zfoo.net.packet.websocket.WebsocketHelloResponse"/>
|
||||
</module>
|
||||
|
||||
<module id="4" name="js" minId="2000" maxId="3000" version="1.0.0">
|
||||
<protocol id="2070" location="com.zfoo.net.packet.websocket.CM_WebSocketPacket" enhance="false"/>
|
||||
<protocol id="2070" location="com.zfoo.net.packet.websocket.WebSocketPacketRequest" enhance="false"/>
|
||||
<protocol id="2071" location="com.zfoo.net.packet.websocket.WebSocketObjectA" enhance="false"/>
|
||||
<protocol id="2072" location="com.zfoo.net.packet.websocket.WebSocketObjectB" enhance="false"/>
|
||||
</module>
|
||||
|
||||
Reference in New Issue
Block a user