Vert.x 3: SockJS 服務代理
Vert.x 3: 服務代理
Vert.x 3: Node和瀏覽器端事件總線重連git
默認初始化的EventBus對象沒有開啓重連, 若是網絡斷開後, 再次調用服務代理會致使錯誤. 所以這裏咱們須要在 onopen 和 onreconnect 兩個事件中處理一下服務註冊的問題.github
var EventBus = require("vertx3-eventbus-client"); var address = "http://localhost:8080/eventbus" var options = { vertxbus_reconnect_attempts_max: Infinity, // Max reconnect attempts vertxbus_reconnect_delay_min: 1000, // Initial delay (in ms) before first reconnect attempt vertxbus_reconnect_delay_max: 5000, // Max delay (in ms) between reconnect attempts vertxbus_reconnect_exponent: 2, // Exponential backoff factor vertxbus_randomization_factor: 0.5 // Randomization factor between 0 and 1 }; var eb = new EventBus(`${address}`, options); eb.enableReconnect(true) # 服務註冊 var QrcodeServiceProxy = require("../lib/qrcode_service-proxy"); eb.onopen = function () { logger.info("Opening event bus...") # 註冊到global對象, 讓其在全部模塊中可用. global.vertx_services = { qrcodeService: new QrcodeServiceProxy(eb, "com.totorotec.servicefactory.qrcode-service") } } eb.onerror = function() { logger.error("Eventbus: error ocurred.") } eb.onclose = function() { logger.error("Eventbus: closed.") } eb.onreconnect = function() { logger.info("Eventbus: reconnecting...") global.vertx_services = { qrcodeService: new QrcodeServiceProxy(eb, "com.totorotec.servicefactory.qrcode-service") } }
殺掉服務端進程, 觀看Node端的輸出:segmentfault
[2017-11-16 16:00:01.821] [ERROR] default - Eventbus: closed. [2017-11-16 16:00:06.837] [ERROR] default - Eventbus: closed. ... ... ...
重啓服務端 yarn run_qr8
後觀察Node的輸出:瀏覽器
... ... ... [2017-11-16 16:00:46.912] [ERROR] default - Eventbus: closed. [2017-11-16 16:00:52.062] [INFO] default - Opening event bus... [2017-11-16 16:00:52.062] [INFO] default - Eventbus: reconnecting...
咱們看到鏈接恢復了.服務器
二維碼項目的全部源代碼, 包括服務器和客戶端在下面的倉庫中:網絡