mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-20 00:24:06 +00:00
feat[json]: 支持json协议格式的服务器
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
@@ -9,14 +8,14 @@
|
||||
* 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.json.client;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.core.json.JsonWebsocketClient;
|
||||
import com.zfoo.net.core.websocket.WebsocketClient;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloRequest;
|
||||
import com.zfoo.net.packet.json.JsonHelloRequest;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import com.zfoo.util.net.HostAndPort;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig;
|
||||
@@ -42,8 +41,8 @@ public class JsonWebsocketClientTest {
|
||||
var client = new JsonWebsocketClient(HostAndPort.valueOf("127.0.0.1:9000"), webSocketClientProtocolConfig);
|
||||
var session = client.start();
|
||||
|
||||
var request = new WebsocketHelloRequest();
|
||||
request.setMessage("Hello, this is the websocket client!");
|
||||
var request = new JsonHelloRequest();
|
||||
request.setMessage("Hello, this is the json client!");
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ThreadUtils.sleep(2000);
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
package com.zfoo.net.core.json.client;
|
||||
|
||||
import com.zfoo.net.packet.json.JsonHelloResponse;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloResponse;
|
||||
import com.zfoo.net.router.receiver.PacketReceiver;
|
||||
import com.zfoo.net.session.model.Session;
|
||||
@@ -31,8 +32,8 @@ public class JsonWsClientController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(JsonWsClientController.class);
|
||||
|
||||
@PacketReceiver
|
||||
public void atWebsocketHelloResponse(Session session, WebsocketHelloResponse response) {
|
||||
logger.info("websocket client receive [packet:{}] from server", JsonUtils.object2String(response));
|
||||
public void atJsonHelloResponse(Session session, JsonHelloResponse response) {
|
||||
logger.info("json client receive [packet:{}] from server", JsonUtils.object2String(response));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
package com.zfoo.net.core.json.server;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.packet.json.JsonHelloRequest;
|
||||
import com.zfoo.net.packet.json.JsonHelloResponse;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloRequest;
|
||||
import com.zfoo.net.packet.websocket.WebsocketHelloResponse;
|
||||
import com.zfoo.net.router.receiver.PacketReceiver;
|
||||
@@ -33,11 +35,11 @@ public class JsonWsServerController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(JsonWsServerController.class);
|
||||
|
||||
@PacketReceiver
|
||||
public void atWebsocketHelloRequest(Session session, WebsocketHelloRequest request) {
|
||||
logger.info("receive [packet:{}] from browser", JsonUtils.object2String(request));
|
||||
public void atJsonHelloRequest(Session session, JsonHelloRequest request) {
|
||||
logger.info("receive [packet:{}] from client", JsonUtils.object2String(request));
|
||||
|
||||
var response = new WebsocketHelloResponse();
|
||||
response.setMessage("Hello, this is the websocket server!");
|
||||
var response = new JsonHelloResponse();
|
||||
response.setMessage("Hello, this is the json server!");
|
||||
|
||||
NetContext.getRouter().send(session, response);
|
||||
}
|
||||
|
||||
@@ -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.json;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class JsonHelloRequest implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 1600;
|
||||
|
||||
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,39 @@
|
||||
/*
|
||||
* 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.json;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class JsonHelloResponse implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 1601;
|
||||
|
||||
private String message;
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
return PROTOCOL_ID;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,9 @@
|
||||
|
||||
<protocol id="1500" location="com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest"/>
|
||||
<protocol id="1501" location="com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse"/>
|
||||
|
||||
<protocol id="1600" location="com.zfoo.net.packet.json.JsonHelloRequest"/>
|
||||
<protocol id="1601" location="com.zfoo.net.packet.json.JsonHelloResponse"/>
|
||||
</module>
|
||||
|
||||
<module id="4" name="js" minId="2000" maxId="3000">
|
||||
|
||||
Reference in New Issue
Block a user