Skip to content

Commit baf4150

Browse files
committed
add websocket protobuf
1 parent 654f7d6 commit baf4150

File tree

12 files changed

+2426
-7
lines changed

12 files changed

+2426
-7
lines changed

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
[submodule "examples/tao"]
55
path = examples/tao
66
url = git@github.com:leesper/tao.git
7-
[submodule "lib/dts-sdk"]
8-
path = lib/dts-sdk
9-
url = git@github.com:Atian-OE/DTSSDK_Golang.git
107
[submodule "submodules/godotenv"]
118
path = submodules/godotenv
129
url = git@github.com:zing-dev/godotenv.git
10+
[submodule "modules/protobuf/protobuf"]
11+
path = modules/protobuf/protobuf
12+
url = https://github.com/golang/protobuf.git

modules/iris-swagger/api/api.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/kataras/iris/v12"
99
)
1010

11-
//
11+
// GetStringByInt
1212
// @Summary Add a new pet to the store
1313
// @Description get string by ID
1414
// @Accept json
@@ -25,7 +25,7 @@ func GetStringByInt(ctx iris.Context) {
2525
_, _ = ctx.WriteString("hello")
2626
}
2727

28-
//
28+
// TestQuick
2929
// @Summary 快速上手接口测试
3030
// @Description 快速上手接口测试,这是我的描述
3131
// @Tags 接口
@@ -66,6 +66,7 @@ type ParamSearch struct {
6666
Name string `json:"name,omitempty" format:"string"`
6767
}
6868

69+
// TestQuickPost
6970
// @Summary 快速上手接口 POST 测试
7071
// @Description 快速上手接口测试,这是我的描述
7172
// @Accept json
@@ -112,6 +113,7 @@ func TestQuickPost(ctx iris.Context) {
112113
})
113114
}
114115

116+
// TestQuickPostForm
115117
// @Summary 快速上手接口 POST Form测试
116118
// @Description 快速上手接口测试,这是我的描述
117119
// @Accept x-www-form-urlencoded
@@ -158,7 +160,8 @@ func TestQuickPostForm(ctx iris.Context) {
158160
})
159161
}
160162

161-
// @Summary 快速上手接口 POST Query测试
163+
// TestQuickPostQuery
164+
//@Summary 快速上手接口 POST Query测试
162165
// @Description 快速上手接口测试,这是我的描述
163166
// @Accept x-www-form-urlencoded
164167
// @Produce json
@@ -198,6 +201,7 @@ func TestQuickPostQuery(ctx iris.Context) {
198201
})
199202
}
200203

204+
// TestQuickFile
201205
// @Summary 快速上手接口 POST File测试
202206
// @Description 快速上手接口测试,这是我的描述
203207
// @Accept mpfd
@@ -222,6 +226,7 @@ func TestQuickFile(ctx iris.Context) {
222226
})
223227
}
224228

229+
// GetStructArrayByString
225230
// @Description get struct array by ID
226231
// @Accept json
227232
// @Produce json
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
node_modules
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const neffos = require('neffos.js');
2+
const protos = require("./user_message_pb.js")
3+
4+
var scheme = document.location.protocol === "https:" ? "wss" : "ws";
5+
var port = document.location.port ? ":" + document.location.port : "";
6+
var wsURL = scheme + "://" + document.location.hostname + port + "/echo";
7+
8+
var outputTxt = document.getElementById("output");
9+
10+
function addMessage(msg) {
11+
outputTxt.innerHTML += msg + "\n";
12+
}
13+
14+
function handleError(reason) {
15+
console.log(reason);
16+
window.alert(reason);
17+
}
18+
19+
function handleNamespaceConnectedConn(nsConn) {
20+
var username = prompt("Please specify a username: ");
21+
22+
let inputTxt = document.getElementById("input");
23+
let sendBtn = document.getElementById("sendBtn");
24+
25+
sendBtn.disabled = false;
26+
sendBtn.onclick = function () {
27+
const input = inputTxt.value;
28+
inputTxt.value = "";
29+
const userMsg = new protos.UserMessage();
30+
userMsg.setUsername(username);
31+
userMsg.setText(input);
32+
userMsg.setAt(new Date().toString());
33+
34+
const body = userMsg.serializeBinary()
35+
// let msg = new neffos.Message();
36+
// msg.Namespace = "default";
37+
// msg.Event = "chat";
38+
// msg.Body = body;
39+
// msg.SetBinary = true;
40+
// nsConn.conn.write(msg);
41+
// ^ ==
42+
nsConn.emitBinary("chat", body);
43+
//
44+
// OR: javascript side will check if body is binary,
45+
// and if it's it will convert it to valid utf-8 text before sending.
46+
// To keep the data as they are, please prefer `emitBinary`.
47+
// nsConn.emit("chat", body);
48+
addMessage("Me: " + input);
49+
};
50+
}
51+
52+
async function runExample() {
53+
// You can omit the "default" and simply define only Events, the namespace will be an empty string"",
54+
// however if you decide to make any changes on this example make sure the changes are reflecting inside the ../server.go file as well.
55+
try {
56+
const conn = await neffos.dial(wsURL, {
57+
default: { // "default" namespace.
58+
_OnNamespaceConnected: function (nsConn, msg) {
59+
addMessage("connected to namespace: " + msg.Namespace);
60+
handleNamespaceConnectedConn(nsConn);
61+
},
62+
_OnNamespaceDisconnect: function (nsConn, msg) {
63+
addMessage("disconnected from namespace: " + msg.Namespace);
64+
},
65+
chat: function (nsConn, msg) { // "chat" event.
66+
console.log(msg);
67+
const userMsg = protos.UserMessage.deserializeBinary(msg.Body);
68+
console.log("at: ", userMsg.getAt());
69+
addMessage(userMsg.getUsername() + " at " + userMsg.getAt() + " : " + userMsg.getText());
70+
},
71+
chat_test: function (nsConn, msg) {
72+
addMessage(new TextDecoder("utf-8").decode(msg.Body));
73+
}
74+
}
75+
});
76+
77+
await conn.connect("default");
78+
79+
} catch (err) {
80+
handleError(err);
81+
}
82+
}
83+
84+
runExample().then(r => {
85+
console.log(r);
86+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!-- the message's input -->
2+
<input id="input" type="text" />
3+
4+
<!-- when clicked then a websocket event will be sent to the server, at this example we registered the 'chat' -->
5+
<button id="sendBtn" disabled>Send</button>
6+
7+
<!-- the messages will be shown here -->
8+
<pre id="output"></pre>
9+
10+
<script src="/bundle.js"></script>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "neffos.js.example.browserify.protobuf",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"browserify": "browserify ./app.js -o ./bundle.js",
6+
"minifyES6": "minify ./bundle.js --outFile ./bundle.js",
7+
"build": "npm run-script browserify && npm run-script minifyES6",
8+
"build-fast": "npm run-script browserify"
9+
},
10+
"dependencies": {
11+
"google-protobuf": "^3.11.1",
12+
"neffos.js": "^0.1.25",
13+
"protobufjs": "~6.8.8"
14+
},
15+
"devDependencies": {
16+
"browserify": "^16.2.3",
17+
"babel-minify": "^0.5.0"
18+
}
19+
}

0 commit comments

Comments
 (0)