Vert.x的Event bus bridge只支持文本格式的协议不支持二进制协议! 同时支持WebSocket的纯文本字符串和JSON格式字符串的消息传递.
参见io.vertx.ext.web.handler.sockjs.impl.SockJSImpl
类的 public Router socketHandler(Handler<SockJSSocket> sockHandler)
方法里
if (enabledTransports.contains(Transport.WEBSOCKET.toString())) {
new WebSocketTransport(vertx, router, sessions, options, sockHandler);
new RawWebSocketTransport(vertx, router, options, sockHandler);
}
纯文本格式 和 JSON格式 的消息类型识别
- 纯文本格式
url里包含了/${server}/${sessionId}
路径的让 io.vertx.ext.web.handler.sockjs.impl.WebSocketTransport
类来处理纯文本消息
- JSON格式
url里不包含/${server}/${sessionId}
路径的让 io.vertx.ext.web.handler.sockjs.impl.RawWebSocketTransport
类来处理JSON消息
相关类,参见如下:
io.vertx.core.http.impl.WebSocketImplBase
io.vertx.ext.web.handler.sockjs.impl.WebSocketTransport
io.vertx.core.http.impl.WebSocketImplBase
收发JSON格式:
连接的url
里不能包含server
和 sessionId
,例如:
/eventbus/websocket
在接收消息的时候整个消息体就是个JSON对象字符串
收发纯文本格式:
连接的url
里必需包含server
和 sessionId
,例如:
/eventbus/${server}/${sessionId}/websocket
server (字符串): 要附加到实际数据连接的 url 的字符串。 默认为随机的 4 位数字。
sessionId (数字): 客户端和服务器都使用会话标识符来区分连接。
具体细节参见SockJS文档
在接收消息的时候根据第1个字节来区分消息的类型(例如o代表是打开连接,a代表消息体是JSON数组).
如下图所示:
EventBusBridgeImpl类
io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl
类是处理 SockJS 桥接 Event Bus的核心类
private void handleSocketClosed(SockJSSocket sock, Map<String, MessageConsumer<?>> registrations) {
clearSocketState(sock, registrations);
checkCallHook(() -> new BridgeEventImpl(BridgeEventType.SOCKET_CLOSED, null, sock));
}
当客户端WebSocket连接断开或者发生异常时clearSocketState方法会清理掉客户端注册的所有Event地址.
private void clearSocketState(SockJSSocket sock, Map<String, MessageConsumer<?>> registrations) {
// On close or exception unregister any handlers that haven't been unregistered
for (MessageConsumer<?> registration : registrations.values()) {
registration.unregister();
checkCallHook(() ->
new BridgeEventImpl(
BridgeEventType.UNREGISTER,
new JsonObject().put("type", "unregister").put("address", registration.address()),
sock));
}
// ensure that no timers remain active
SockInfo info = sockInfos.remove(sock);
if (info != null) {
PingInfo pingInfo = info.pingInfo;
if (pingInfo != null) {
vertx.cancelTimer(pingInfo.timerID);
}
}
}
如下图所示:

<<<<<< [完] >>>>>>