Skip to content

Commit 4359536

Browse files
feat: add support for the msgpack parser
Related: #28
1 parent 925c617 commit 4359536

8 files changed

Lines changed: 16881 additions & 108 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Server } from "socket.io";
2+
import { instrument } from "../../dist/index.js";
3+
import parser from "socket.io-msgpack-parser";
4+
5+
const io = new Server(3000, {
6+
cors: {
7+
origin: ["https://admin.socket.io", "http://localhost:8080"],
8+
credentials: true,
9+
},
10+
parser,
11+
});
12+
13+
instrument(io, {
14+
auth: false,
15+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "single-server-msgpack-parser",
3+
"version": "0.0.1",
4+
"description": "Sample server to be used with the Socket.IO Admin UI",
5+
"private": true,
6+
"main": "index.js",
7+
"type": "module",
8+
"scripts": {
9+
"start": "node index.js"
10+
},
11+
"author": "Damien Arrachequesne <damien.arrachequesne@gmail.com>",
12+
"license": "MIT",
13+
"dependencies": {
14+
"socket.io": "^4.4.1",
15+
"socket.io-msgpack-parser": "^3.0.1"
16+
}
17+
}

ui/package-lock.json

Lines changed: 16801 additions & 100 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"core-js": "^3.6.5",
13+
"socket.io-msgpack-parser": "^3.0.1",
1314
"vue": "^2.6.11",
1415
"vue-i18n": "^8.22.3",
1516
"vue-router": "^3.2.0",
@@ -33,7 +34,7 @@
3334
"prettier": "^2.2.1",
3435
"sass": "^1.32.0",
3536
"sass-loader": "^10.0.0",
36-
"socket.io-client": "^4.0.1",
37+
"socket.io-client": "^4.5.0",
3738
"vue-chartjs": "^3.5.1",
3839
"vue-cli-plugin-i18n": "~2.0.3",
3940
"vue-cli-plugin-vuetify": "~2.3.1",

ui/src/App.vue

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
:initial-server-url="serverUrl"
1818
:initial-ws-only="wsOnly"
1919
:initial-path="path"
20+
:initial-parser="parser"
2021
:is-connecting="isConnecting"
2122
:error="connectionError"
2223
@submit="onSubmit"
@@ -28,6 +29,7 @@
2829
import AppBar from "./components/AppBar";
2930
import NavigationDrawer from "./components/NavigationDrawer";
3031
import { io } from "socket.io-client";
32+
import msgpackParser from "socket.io-msgpack-parser";
3133
import ConnectionModal from "./components/ConnectionModal";
3234
import SocketHolder from "./SocketHolder";
3335
import { mapState } from "vuex";
@@ -63,6 +65,7 @@ export default {
6365
serverUrl: (state) => state.connection.serverUrl,
6466
wsOnly: (state) => state.connection.wsOnly,
6567
path: (state) => state.connection.path,
68+
parser: (state) => state.connection.parser,
6669
backgroundColor: (state) =>
6770
state.config.darkTheme ? "" : "grey lighten-5",
6871
}),
@@ -84,7 +87,7 @@ export default {
8487
},
8588
8689
methods: {
87-
tryConnect(serverUrl, auth, wsOnly, path) {
90+
tryConnect(serverUrl, auth, wsOnly, path, parser) {
8891
this.isConnecting = true;
8992
if (SocketHolder.socket) {
9093
SocketHolder.socket.disconnect();
@@ -98,6 +101,7 @@ export default {
98101
withCredentials: true, // needed for cookie-based sticky-sessions
99102
transports: wsOnly ? ["websocket"] : ["polling", "websocket"],
100103
path,
104+
parser: parser === "msgpack" ? msgpackParser : null,
101105
auth,
102106
});
103107
socket.once("connect", () => {
@@ -110,6 +114,7 @@ export default {
110114
serverUrl,
111115
wsOnly,
112116
path,
117+
parser,
113118
});
114119
SocketHolder.socket = socket;
115120
this.registerEventListeners(socket);
@@ -121,10 +126,16 @@ export default {
121126
if (this.isConnecting || err.message === "invalid credentials") {
122127
this.showConnectionModal = true;
123128
this.connectionError = err.message;
124-
this.isConnecting = false;
125129
}
130+
this.isConnecting = false;
126131
});
127-
socket.on("disconnect", () => {
132+
socket.on("disconnect", (reason) => {
133+
// this should not be needed, but connection errors with a mismatching parser may trigger in a "disconnect"
134+
// event instead of a "connect_error" event (needs to be fixed in the client code)
135+
if (this.isConnecting) {
136+
this.isConnecting = false;
137+
this.connectionError = reason;
138+
}
128139
this.$store.commit("connection/disconnect");
129140
});
130141
},
@@ -171,7 +182,8 @@ export default {
171182
password: form.password,
172183
},
173184
form.wsOnly,
174-
form.path
185+
form.path,
186+
form.parser
175187
);
176188
},
177189
},
@@ -187,7 +199,8 @@ export default {
187199
sessionId,
188200
},
189201
this.wsOnly,
190-
this.path
202+
this.path,
203+
this.parser
191204
);
192205
} else {
193206
this.showConnectionModal = true;

ui/src/components/ConnectionModal.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
:label="$t('connection.path')"
3838
></v-text-field>
3939

40+
<v-select
41+
v-model="parser"
42+
:label="$t('connection.parser')"
43+
:items="parserOptions"
44+
/>
45+
4046
<v-btn
4147
:loading="isConnecting"
4248
:disabled="isConnecting || !isValid"
@@ -63,6 +69,7 @@ export default {
6369
initialServerUrl: String,
6470
initialWsOnly: Boolean,
6571
initialPath: String,
72+
initialParser: String,
6673
error: String,
6774
},
6875
@@ -73,6 +80,17 @@ export default {
7380
path: this.initialPath,
7481
username: "",
7582
password: "",
83+
parser: this.initialParser,
84+
parserOptions: [
85+
{
86+
value: "default",
87+
text: this.$t("connection.default-parser"),
88+
},
89+
{
90+
value: "msgpack",
91+
text: this.$t("connection.msgpack-parser"),
92+
},
93+
],
7694
};
7795
},
7896
@@ -95,6 +113,7 @@ export default {
95113
path: this.path,
96114
username: this.username,
97115
password: this.password,
116+
parser: this.parser,
98117
});
99118
},
100119
},

ui/src/locales/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
"invalid-credentials": "Invalid credentials",
2525
"error": "Error",
2626
"websocket-only": "WebSocket only?",
27-
"path": "Path"
27+
"path": "Path",
28+
"parser": "Parser",
29+
"default-parser": "Built-in parser",
30+
"msgpack-parser": "MessagePack parser"
2831
},
2932
"dashboard": {
3033
"title": "Dashboard"

ui/src/store/modules/connection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default {
66
serverUrl: "",
77
wsOnly: false,
88
path: "/socket.io",
9+
parser: "default",
910
sessionId: "",
1011
connected: false,
1112
},
@@ -16,16 +17,19 @@ export default {
1617
state.wsOnly = localStorage.getItem("ws_only") === "true";
1718
state.sessionId = localStorage.getItem("session_id");
1819
state.path = localStorage.getItem("path") || "/socket.io";
20+
state.parser = localStorage.getItem("parser") || "default";
1921
}
2022
},
21-
saveConfig(state, { serverUrl, wsOnly, path }) {
23+
saveConfig(state, { serverUrl, wsOnly, path, parser }) {
2224
state.serverUrl = serverUrl;
2325
state.wsOnly = wsOnly;
2426
state.path = path;
27+
state.parser = parser;
2528
if (isLocalStorageAvailable) {
2629
localStorage.setItem("server_url", serverUrl);
2730
localStorage.setItem("ws_only", wsOnly);
2831
localStorage.setItem("path", path);
32+
localStorage.setItem("parser", parser);
2933
}
3034
},
3135
saveSessionId(state, sessionId) {

0 commit comments

Comments
 (0)