我将尝试在不附加任何代码的情况下解释我的问题,我认为这不是必需的。好的,我在 JS 中有一个连接到我的 java 服务器的 websocket 客户端。握手完成,在客户端调用连接的处理程序,所以我向服务器发送一条消息,读取该消息。然后消息被反转并发送回客户端,但不调用客户端消息接收处理程序或任何其他处理程序。
这是我发送给客户的消息:
b[0]=-127;//Its the same of 129?
b[1]=1;
b[2]=18;//any char..
我认为问题一定出在第一个字节上。我写“b[0] = (byte)129;” 但是当我读到它时它返回-127,也许是因为,¿字节129必须是无符号的?感谢您的帮助:P
请求的客户端代码:
<html><head><meta charset="utf-8">
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:10637/penise";
var output;
function init()
{
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket()
{
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt)
{
writeToScreen("CONNECTED");
var msg = String.fromCharCode(1)+ String.fromCharCode(0)+"This is niceeee"
doSend(msg);
}
function onClose(evt)
{
writeToScreen("DISCONNECTED");
}
function onMessage(evt)
{
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
websocket.close();
}
function onError(evt)
{
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message)
{
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message)
{
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
function char(i) {
return String.fromCharCode(i);
}
</script>
</head><body><h2>WebSocket Test</h2>
<div id="output"><p style="word-wrap: break-word;">CONNECTED</p><p style="word-wrap: break-word;">SENT: This is niceeee</p><p style="word-wrap: break-word;"><span style="color: red;">ERROR:</span> undefined</p><p style="word-wrap: break-word;">DISCONNECTED</p></div>
</body></html>