WebWorker與WebSocket實現前端消息總線


Web Worker讓JS有了多線程的能力,可以將復雜耗時的操作都交付給Worker線程處理。WebSocket讓web端與服務端維持一個有效的長連接,實現服務端主動推送數據。將二者一結合,業務系統信息流轉通知功能完全就可以剝離出來。

架構圖

Alt text

JS Worker

Worker工作在一個專用的作用域DedicatedWorkerGlobalScope,在這個作用域中,不能直接操作DOM節點,不能使用Window對象的默認方法和屬性。不過對於網絡的訪問是完全沒有問題的。具體能使用那些對象和方法請點擊這里查看

從上圖中可明顯的看出,Worker在當前架構中實現一個橋梁的左右,上連接socket端中的數據,下負責分發socket中的數據。此處我們先了解下Worker本身的功能實現。

  1. 主線程與Worker線程通過方法postMessage相互傳遞信息
  2. 主線程與Worker線程通過事件onmessage接收相互傳遞的消息
  3. Worker中引入第三方js使用方法importScripts([url,])
  4. 主線程調用worker.terminate()結束線程
  5. Worker線程通過調用this.close()結束自身線程

新建一個webworker.js文件,並在其中編寫如下代碼

//author:herbert qq:464884492
onmessage = function (event) {
    if (event.data.code) {
        var code = event.data.code.toLowerCase();
        switch (code) {
            case "init":
                var userId = event.data.loggedUserId;
                var sessionId = event.data.sessionid;
                if (!sessionId) {
                    this.close();
                    return;
                }
                postMessage({ code: "codeone", msg: "你好,組件1" });
                postMessage({ code: "codetwo", msg: "你好,組件2" });
                break;
            default:
                break;
        }
    }
}

注意:在 onmessage 前不能加var否則在IE下會接收不了消息。IE總是讓你充滿挫敗感!!

新建一個index.html頁面,在script塊中編寫以下代碼,實現與webworker.js通訊

//author:herbert qq:464884492
var work = new Worker('webworker.js')
    , textone = document.querySelector("#textone")
    , textTwo = document.querySelector("#texttwo")
textAll = document.querySelector("#textAll");
work.onmessage = function (event) {
    var data = event.data;
    if (!!data.code) {
        switch (data.code) {
            case "close":
                work.terminate();
            case "codeone":
                textone.value = textone.value + JSON.stringify(data) + "\r\n";
                textAll.value = textAll.value + JSON.stringify(data) + "\r\n";
                break;
            case "codetwo":
                textTwo.value = textTwo.value + JSON.stringify(data) + "\r\n";
                textAll.value = textAll.value + JSON.stringify(data) + "\r\n";
                break;
            default:
                textAll.value = textAll.value + JSON.stringify(data) + "\r\n";
        }
    }
};
work.postMessage({
    code: "init",
    loggedUserId: 'demo',
    sessionid: 'demo'
});

JS WebSocket

WebSocket和Http一樣都是基於Tcp協議。不同是WebSocket實現了服務端與客戶端的全雙工通訊。在Websocket未出現之前,要是實現一個信息推送的功能,通過http來實現唯一方案就是輪訓,輪訓分長短,各有弊端。現在WebSocket一出現,一切都好辦了。

接下來我們開始建立一個WebSocket連接

方法中的root表示當前作用域,在主線程是root=window,在WebWorker線程root=DedicatedWorkerGlobalScope

//author:herbert qq:464884492
var root = this, socket = null;
function connect(wsurl) {
    if ('WebSocket' in root) {
        socket = new WebSocket(wsurl);
    } else if ('MozWebSocket' in root) {
        socket = new MozWebSocket(wsurl);
    } else {
        alert("您的瀏覽器版本過低,將不能接收系統消息");
    }
}

wsurl格式為ws:\\ 或者 wss:\\,后者表示SSL加密傳輸。實際地址如: ws://localhost:8090/demo/demowebsocket
接下來,我們需要為socket處理事件,負責接收服務端推送的消息

//author:herbert qq:464884492
function onOpen() {
    postMessage({ code: "openConnect" });
}
function onClose() {
    postMessage({ code: "closewsconnect" });
}
function onMessaage(event) {
    postMessage(JSON.parse(event.data));
}
function onError(event) {
    socket = null;
    if (event.target.readyState == 3) {
        //斷線重連
        setTimeout(function () {
            connect(event.target.url);
            initMessageEvent();
        }, 1000);
    }
}
function sendMessage(msg) {
    if (socket == null) return;
    socket.send(msg);
}
function initMessageEvent() {
    socket.onopen = onOpen; //socket連接成功處理事件
    socket.onclose = onClose; //socket連接關閉處理事件
    socket.onmessage = onMessaage; //socket接收到新消息
    socket.onerror = onError; //soket錯誤處理事件
}

JAVA WebSocket

Tomcat7x已經實現了標准WebScoket接口,在項目中只需要編寫一個普通的實體bean配置注解就可以實現一個標准的WebSocket Api。開發中主要使用一些注解

  • @ServerEndpoint 設置WebSocket連接地址,以及url參數
    如: @ServerEndpoint(value = “/demowebsocket/{userId}/{sessionId}”),其中{userId}、{sessionId} 為pathParam可以在onOpen函數中通過函數參數 @PathParam 獲取
  • @PathParam 獲取URL地址上對應的注解參數
  • @OnOpen 建立連接注解
  • @OnClose 關閉連接注解
  • @OnMessage 接收消息注解
  • @OnError 錯誤注解

被注解約束的函數都可以任意選擇需要的參數,可選擇的參數有 Session、EndpointConfig 以及 @PathParam, 服務端Bean代碼如下

//author:herbert qq:464884492
@ServerEndpoint(value = "/demowebsocket/{userId}/{sessionId}")
public class DemoWebSokcet {
    private static final Set<DemoWebSokcet> connections = new CopyOnWriteArraySet<DemoWebSokcet>();
    private Session session;
    public DemoWebSokcet() {
    }

    @OnOpen
    public void openConnection(Session session, EndpointConfig conf,
        @PathParam("userId") String userId,
        @PathParam("sessionId") String sessionId) {
        this.session = session;
        connections.add(this);
        JSONObject jo = new JSONObject();
        jo.put("code", "newuser");
        jo.put("userid", userId);
        jo.put("sessionid", sessionId);
        jo.put("msg", "server:新連接用戶");
        sendMessage(jo);

        // 測試 代碼
        JSONObject jo1 = new JSONObject();
        jo1.put("code", "codeone");
        jo1.put("userid", userId);
        jo1.put("sessionid", sessionId);
        jo1.put("msg", "Server:組件1你好");
        sendMessage(jo1);

        JSONObject jo2 = new JSONObject();
        jo2.put("code", "codetwo");
        jo2.put("userid", userId);
        jo2.put("sessionid", sessionId);
        jo2.put("msg", "server:組件2你好");
        sendMessage(jo2);
    }

    @OnClose
    public void closeConnection(@PathParam("userId") String userId,
        @PathParam("sessionId") String sessionId) {
        connections.remove(this);
        JSONObject jo = new JSONObject();
        jo.put("code", "connectionClose");
        jo.put("userid", userId);
        jo.put("sessionid", sessionId);
        jo.put("msg", "server:連接關閉");
        sendMessage(jo);
    }

    // 處理文本消息
    @OnMessage
    public void handleTextMsg(Session session, String message,
        @PathParam("userId") String userId,
        @PathParam("sessionId") String sessionId) {
        System.out.println("userId=>" + userId + " sessionId=>" + sessionId);
        // 原樣轉發客戶端消息
        sendMessage(JSONObject.parseObject(message));
    }

    // 處理二進制消息
    @OnMessage
    public void handleBinaryMsg(Session session, ByteBuffer msg,
        @PathParam("userId") String userId,
        @PathParam("sessionId") String sessionId) {

    }

    // 處理pong消息
    @OnMessage
    public void handlePongMsg(Session session, PongMessage msg,
        @PathParam("userId") String userId,
        @PathParam("sessionId") String sessionId) {
        JSONObject jo = new JSONObject();
        jo.put("code", "pong");
        jo.put("userid", userId);
        jo.put("sessionid", sessionId);
        jo.put("msg", msg.getApplicationData().toString());
        sendMessage(jo);
    }

    @OnError
    public void onError(Throwable t, @PathParam("userId") String userId,
        @PathParam("sessionId") String sessionId) throws Throwable {
    JSONObject jo = new JSONObject();
    jo.put("code", "servererror");
    jo.put("userid", userId);
    jo.put("sessionid", userId);
    jo.put("msg", t.getMessage());
    sendMessage(jo);
}
 
private static void sendMessage(JSONObject msg) {
    for (DemoWebSokcet client : connections) {
        try {
            synchronized(client) {
                client.session.getBasicRemote()
                    .sendText(msg.toJSONString());
            }
        } catch (IOException e) {
            JSONObject jo = new JSONObject();
            jo.put("code", "servererror");
            jo.put("userid",
                client.session.getPathParameters().get("userid"));
            jo.put("sessionid",
                client.session.getPathParameters().get("sessionid"));
            connections.remove(client);
            try {
                client.session.close();
            } catch (IOException e1) {
            }

            jo.put("msg", "server:發送消息出現異常,連接已關閉" + e.getMessage());
            sendMessage(jo);
        }
    }
}
}

在測試代碼編寫過程中,通過pom方式引入javax.websocket-api,啟動后始終出現 Error during WebSocket handshake: Unexpected response code: 404連接錯誤,后來通過直接件tomcat/bin下對應的tomcat實現的jar復制到webapp對應的bin文件夾下解決問題。

Demo預覽

Alt text

總結

篇幅比較長,讀到這里也不容易!WebWorker和WebSocket我也是第一次將二者結合起來。感覺現在javascript功能真的是越來越豐富了。demo地址,還有一點感悟,對於開發中的新知識點,首先你得學會怎么用,其次在通過閱讀源碼,以及理論知識讓你使用的更順利,甚至改變它。

有喜歡聊技術朋友也歡迎入群,若二維碼失效可加我微信回復**前端**


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM