I'm trying to create a simple C# websocket server with websocket-sharp and I followed the official guide but I got some peculiar reply instead.
C# Snippet
using System;
using WebSocketSharp.Server;
namespace WebSocketSharp
{
class MainClass
{
public static void Main(string[] args)
{
startServer();
}
public static void startServer()
{
var ws = new WebSocketServer(System.Net.IPAddress.Any, 8181);
ws.Start();
Console.WriteLine("Server started");
Console.ReadKey(true);
ws.Stop();
Console.WriteLine("Server stopped");
}
}
}
When I tried to connect my client (HTML) to the server, I got a message
WebSocket connection to 'ws://127.0.0.1:8181/' failed: Error during WebSocket handshake: Unexpected response code: 501
and this showed up at my terminal at the same time
According to HTTP Protocol, code 501 means
The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.
This is my client code and I believe it conforms with the request method.
<!doctype html>
<head>
</head>
<body>
<form>
<input id="textField1" type="text" style="margin: 0 5px; width: 200px; height: 40px;" placeholder="Enter data">
<button id="sendTextField1" type="button" class="btn btn-info">Send</button>
</form>
<script type="text/javascript">
var start = function () {
var wsImpl = window.WebSocket || window.MozWebSocket;
window.ws = new wsImpl('ws://127.0.0.1:8181/');
ws.onmessage = function (evt) {
console.log(evt.data);
};
ws.onopen = function () {
console.log("Connected");
};
ws.onclose = function () {
console.log("Closed");
};
document.getElementById("sendTextField1").onclick = function() {
sendToSocket(document.getElementById('textField1').value);
};
function sendToSocket(value){
console.log("Sending value to socket " + value + " ");
ws.send(value);
};
}
window.onload = start;
Can anyone tell me where is my mistake?