記錄一下...
var _ws_received_msg; var _ws; var _strResult = ""; var _arrResult; function myWebSocket(websocket_url) { if ("WebSocket" in window) { _ws = new WebSocket(websocket_url); _ws.onopen = function() { alert("connected"); }; _ws.onmessage = function (evt) { _ws_received_msg = evt.data; ////////////////////////////////////////////// // process web socket data as binary string ////////////////////////////////////////////// fr = new FileReader(); function receivedBinary() { // you can process fr.result here :D _strResult = showResult2(fr, "Binary"); _arrResult = _strResult.split(","); } fr.onload = receivedBinary; fr.readAsBinaryString(_ws_received_msg); ////////////////////////////////////////////// // process web socket data as text ////////////////////////////////////////////// fr = new FileReader(); function receivedText() { // you can process fr.result here :D } fr.onload = receivedText; fr.readAsText(_ws_received_msg); }; _ws.onclose = function() { alert("close"); }; } else { alert("WebSocket NOT supported by your Browser!"); } }