我正在尝试从Websockets解析帧。我用Fiddler做的,它向我展示了帧中的二进制数据。我需要同样的结果,但我需要通过Javascript或Php(或任何其他语言)获得它。我试着用Js 做到这一点
var a = new WebSocket("ws://example.com");
a.onopen = function()
{
console.log("open");
a.send("test");
var b = new Uint8Array([8,6,7,5,3,0,9]);
a.send(b.buffer); };
a.onmessage = function(e) { console.log(e.data.toString());};
a.onclose = function() { console.log("closed");};
但我没有收到任何关于"a.onmessage"的数据。这一刻我卡住了。再次澄清我的问题。我需要一个简单的代码示例来解析来自websocket的帧。我试图解析的示例
谢谢!
使用DataView:
var socket = new WebSocket('ws://127.0.0.1:8081');
socket.binaryType = 'arraybuffer';
socket.onmessage = function (e) {
var data = e.data;
var dv = new DataView(data);
// reads an UInt16 at the beginning
var uint16= dv.getUint16(0);
// reads next UInt16 (sets 2 bytes of offset, since previous UInt16 takes 2 bytes)
var another_uint16= dv.getUint16(0);
};