-
StackOverflow 文档
-
JavaScript 教程
-
WebSockets
-
使用二进制消息
var wsHost = "http://my-sites-url.com/path/to/echo-web-socket-handler";
var ws = new WebSocket(wsHost);
var buffer = new ArrayBuffer(5); // 5 byte buffer
var bufferView = new DataView(buffer);
bufferView.setFloat32(0, Math.PI);
bufferView.setUint8(4, 127);
ws.binaryType = 'arraybuffer';
ws.onmessage = function(message) {
var view = new DataView(message.data);
console.log('Uint8:', view.getUint8(4), 'Float32:', view.getFloat32(0))
};
ws.onopen = function() {
ws.send(buffer);
};