我知道所有内容都在websocket中作为字符串传递,但我有这两个函数:
function read_string(id)
{
if (recvpos[id][recvbufi[id]] >= recvsize[id][recvbufi[id]])
recvbufi[id]++;
if (recvbufi[id] >= recvbuf[id].length)
return "";
var str = new String("");
while(1)
{
var buf = new Uint8Array(recvbuf[id][recvbufi[id]], recvpos[id][recvbufi[id]]+2, 1);
recvpos[id][recvbufi[id]] += 1;
if (buf[0] == 0)
return str;
str += String.fromCharCode(buf[0]);
}
}
function recv(id)
{
if (sockets[id].readyState == 2 || sockets[id].readyState == 3)
return 0;
if (recvoncesize[id].length == 0 || sockets[id].readyState == 0)
return -1;
return recvoncesize[id].pop();
}
我在C#控制台应用程序上使用Fleck连接并且所有事件都能正常工作,连接,断开连接,甚至将数据发送到服务器,但服务器回复这些功能都不起作用,即使recv返回-1断开连接,因为它不会返回消息的大小。
提前致谢。
编辑:这是连接过程的外观:
function connect(serverip, serverport)
{
if (getBrowser() == 2)
sockets.push(new MozWebSocket("ws://"+serverip+":"+serverport));
else
sockets.push(new WebSocket("ws://"+serverip+":"+serverport));
var id = sockets.length - 1;
recvbuf[id] = new Array();
recvbufi[id] = 0;
recvsize[id] = new Array();
recvoncesize[id] = new Array();
recvpos[id] = new Array();
sendbuf[id] = new ArrayBuffer(65535);
sendsize[id] = 0;
if (getBrowser() == 1)
sockets[id].binaryType = "arraybuffer";
sockets[id].onmessage = function(event)
{
if (getBrowser() == 1)
rbuf = event.data;
if (getBrowser() == 2)
rbuf = Base64ToArrayBuffer(event.data);
var buf = new Uint8Array(rbuf, 0, 2);
var wb = new Uint8Array(buf).buffer;
var word = new Uint16Array(wb);
recvbuf[id].push(rbuf);
recvsize[id].push(word[0]);
recvoncesize[id].push(word[0]);
recvpos[id].push(0);
}
return id;
}