王智勇 2e0fc1b260 feat:websocket支持 | 4 years ago | |
---|---|---|
dim-client | 4 years ago | |
dim-common | 4 years ago | |
dim-service | 4 years ago | |
.gitignore | 5 years ago | |
README.md | 4 years ago | |
pom.xml | 4 years ago |
基于 netty 的服务中间件
实现客户端和服务端的tcp长链接通信 完成数据交换
RPC IM
字段 | 类型 | 说明 | 示例 | 是否必填 |
---|---|---|---|---|
command | string | 命令 | LOGIN_REQUEST | 是 |
data | string | token | 登录接口获取access_token | 是 |
字段 | 类型 | 说明 | 示例 | 是否必填 |
---|---|---|---|---|
command | int | 命令 | 2(登录返回) | 是 |
success | boolean | 是否成功 | true | 是 |
reason | string | 原因 | token错误,连接失败 | 否 |
字段 | 类型 | 说明 | 示例 | 是否必填 |
---|---|---|---|---|
command | int | 命令 | 91(消息推送) | 是 |
data | 泛型 | 根据命令的数据 | 是 | |
id | string | 消息id | 是 |
字段 | 类型 | 说明 | 示例 | 是否必填 |
---|---|---|---|---|
command | string | 命令 | CONFIRM | 是 |
data | string | message_id | 消息体id | 是 |
请求示例:
{
"command":"LOGIN_REQUEST",
"data":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjI1NTk1NDY5MDMsInVzZXJfbmFtZSI6ImpnemgwMSIsImF1dGhvcml0aWVzIjpbIlJPTEVfT1BFUkFUT1IiXSwianRpIjoiZjA2NmQxMGMtZWVmMy00Mzk4LTg0NjctOTNiNGYwNWRiMTEwIiwiY2xpZW50X2lkIjoiYW5waW4tb3BzLXBjIiwic2NvcGUiOlsic2VsZWN0Il19.frUrkTEJaIwo-ZeMbs3L9UNmxr26WHOi52TGQWnSu00"
}
{
"command":"CONFIRM",
"data":"59232dfc59d24eb2868e740afc7074ae"
}
返回示例:
{
"command": 2,
"session": {
"platform": "1",
"sessionId": "ec89a6f9a8cc4d8291bffe0f3aa187e6",
"userId": "jgzh01"
},
"success": true
}
{
"command": 30,
"data": {
"A": 1,
"B": "vvv"
},
"id": "999c2f55176c4136ba6a4b9ac28f9fc4"
}
代码示例:
<html><head><title>Web Socket Test</title></head>
<body>
<script type="text/javascript">
var socket;
if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}
if (window.WebSocket) {
socket = new WebSocket("ws://127.0.0.1:9010/websocket");
socket.onmessage = function(event) {
var ta = document.getElementById('responseText');
ta.value = ta.value + '\n' + event.data
};
socket.onopen = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket opened!";
};
socket.onclose = function(event) {
var ta = document.getElementById('responseText');
ta.value = ta.value + "Web Socket closed";
};
} else {
alert("Your browser does not support Web Socket.");
}
function send(message) {
if (!window.WebSocket) { return; }
if (socket.readyState == WebSocket.OPEN) {
socket.send(message);
} else {
alert("The socket is not open.");
}
}
</script>
<form onsubmit="return false;">
<input type="text" name="message" value="Hello, World!"/><input type="button" value="Send Web Socket Data"
onclick="send(this.form.message.value)" />
<h3>Output</h3>
<textarea id="responseText" style="width:500px;height:300px;"></textarea>
</form>
</body>
</html>