Reputation: 167
How to create a simple HTML5 Websockets application using ASP.NET 4.0 (C#). My problem is about creating Websockets. Below is the code which is tried...
try
{
var listener = new TcpListener(IPAddress.Loopback, 9988);
listener.Start();
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
writer.WriteLine("Upgrade: WebSocket");
writer.WriteLine("Connection: Upgrade");
writer.WriteLine("WebSocket-Origin: http://localhost:9988");
writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
writer.WriteLine("");
}
listener.Stop();
}
catch (Exception ex)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", ex.Message);
}
And the HTML code as follows...!
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
var status = '';
function conStatus(state) {
return (state == 0) ? 'Connecting..!' : (state == 1) ? 'Opened..!' : (state == 2) ? 'Closing..!' : 'Closed..!';
}
function WebSocketTest() {
if ("WebSocket" in window) {
try {
debugger;
var connection = new WebSocket('ws://localhost:9988');
status += '<br/>Socket Status: ' + conStatus(connection.readyState);
connection.onopen = function () {
status += 'Socket Opened..!';
};
connection.onmessage = function () {
status += 'Socket received a message..!';
};
connection.onclose = function () {
status += 'Socket Closed..!';
};
connection.send('Hello World..!');
}
catch (exception) {
status += '<br/>EXCEPTION: ' + exception;
}
}
else {
status += "Your Browser does not support WebSockets...!"
}
document.write(status);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="sse">
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
<div id="status">
</div>
</form>
</body>
</html>
If i run this file, the page keeps onloading.. i cant see any result..! Kindly provide me a solution.... Thanks in advance....
Upvotes: 0
Views: 1303
Reputation: 1485
Websocket implementation requires more than a simple TcpListener. You may try PokeIn in order to have WebSocket support on all the platforms or you may just update your solution to ASP.NET 4.5 and stick with IIS 8
Upvotes: 1
Reputation: 42205
You'll need to make a few changes to your server. It'd be best to start by reading the handshaking section of the spec. At a minimum, you'll need to add a Sec-WebSocket-Accept header to your response. You could also consider checking Sec-WebSocket-Version and Sec-WebSocket-Protocol in the client request.
After this, you should have a server that'll complete its handshake then immediately close its connection. You should keep your tcp client alive indefinitely, closing it only when the client closes or requests a close or the server wants to shutdown.
You'll also need to update your client code. The initial handshake prompted by new WebSocket(...)
is asynchronous. You can't use the connection variable until the onopen
callback runs (so move the connection.send('Hello World..!')
line there).
After that, you should read the data framing section of the spec to see how to send/receive messages.
This adds up to a reasonable chunk of work. You might find it quicker to use an existing open source server instead. I haven't used it myself but Fleck seems to be well regarded and looks like it should be easy to use.
Upvotes: 1