我正在体验'看起来'是一个独特的问题。我现在有一个客户端/服务器模型从Windows窗体中获取输入并将其推送到我从头开始编写的服务器,从那里,它应该通过websocket被拉到网页。
我刚刚克服了握手并且正在有效地建立连接;然而,我的onMessage方法似乎没有被击中(无论我多久/晚发送文本),它看起来好像理论上会触发,还有其他事情发生......
当我从发送输入到服务器的客户端发送文本时,它会传送第一行并等待第二行。服务器接收此输入并将其写入所有连接的“输出”客户端。这意味着包括我的websocket。这是我迷路的地方。第一行被“写入”(根据服务器),但“onmessage”事件永远不会发生。如果我向服务器发送第二行,则在尝试写入时,服务器会抛出“无法将数据写入传输连接”的范围:已建立的连接已被主机中的软件中止。并且客户端触发onclose。
也许有人可以提供一些见解。这是我的websocket代码:
<!DOCTYPE html>
<meta charset="utf-8" />
<html>
<head>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:9001";
var output;
function init()
{
writeToScreen("Starting");
testWebSocket();
}
function testWebSocket()
{
websocket = new WebSocket(wsUri);
websocket.async = false;
writeToScreen("Instantiated");
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
writeToScreen("Evented");
//onOpen(null);
}
function onOpen(evt)
{
try
{
writeToScreen("CONNECTED");
}
catch( e )
{
writeToScreen(e);
}
}
function onClose(evt)
{
writeToScreen("DISCONNECTED");
}
function onMessage(evt)
{
try
{
writeToScreen("Message");
writeToScreen("<span style=\"color: blue;\">RESPONSE: " + evt.data+"</span>");
}
catch( e )
{
writeToScreen(e);
}
}
function onError(evt)
{
writeToScreen("<span style=\"color: red;\">ERROR:</span> " + evt.data);
}
function doSend(message)
{
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message)
{
output = document.getElementById("output");
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
</script>
</head>
<body>
<div id="output">
</div>
</body>
</html>
及其相应的输出:
Starting
Instantiated
Evented
CONNECTED
DISCONNECTED
这是服务器'Write'方法(从我写的一个类继承自:)
public virtual void WriteLine(String Argument)
{
if (this.ClientStream == null)
{
try
{
this.ClientStream = new NetworkStream(this.ClientSocket);
this.WriteLine(Argument);
return;
}
catch (Exception e)
{
throw new Exception("Exception encountered while writting to Client.\n" + e.Message);
}
}
else if (this.OutputWriter == null)
{
try
{
this.OutputWriter = new StreamWriter(this.ClientStream);
this.WriteLine(Argument);
return;
}
catch (Exception e)
{
throw new Exception("Exception encountered while writting to Client.\n" + e.Message);
}
}
else
{
try
{
this.OutputWriter.Write(Argument + "\r\n");
this.OutputWriter.Flush();
return;
}
catch (Exception e)
{
throw new Exception("Exception encountered while writting to Client.\n" + e.Message);
}
}
}
服务器'连接'方法(来自我继承的类):
public void Start()
{
if (this.Server == null)
{
throw new Exception("Could not start TelnetServer.Listener, Server is null.");
}
else if (this.RunningThread == null)
{
this.RunningThread = new Thread(this.Start);
this.RunningThread.Name = "TelnetServer.Listener:" + this.Port;
this.RunningThread.Start();
return;
}
else if (this.LocalAddress == null)
{
this.LocalAddress = IPAddress.Any;
this.Start();
return;
}
else if (this.NetworkListener == null)
{
this.NetworkListener = new TcpListener(this.LocalAddress, this.Port);
this.NetworkListener.Start();
this.Start();
return;
}
else
{
this.RunningFlag = true;
while (this.IsRunning())
{
try
{
Socket NewSocket = this.NetworkListener.AcceptSocket();
this.Server.Connect(NewSocket);
continue;
}
catch (Exception e)
{
this.Print("Exception encountered while creating new Client.");
this.Print(e.Message);
continue;
}
}
}
}
如果需要更多信息,我很乐意详细说明。请注意,当我得到一个新连接时,我在'final'类中完成一次完整的握手,并且最后一个类中的'write'方法将消息发送到上面提到的write方法。
提前再次感谢。
[编辑] 添加发送帧;现在它在第一条消息上失败了。 if(HeaderFinished&amp;&amp; HeaderSent) base.WriteLine(“\ u0000”+ Encoding.UTF8.GetBytes(Argument)+“\ uffff”); 其他 base.WriteLine(参数); [/编辑]