private async Task ProcessWebsocketSession(WebSocketContext wsContext)
{
//### Receiving
// Define a maximum message size this handler can receive (1K in this case) and allocate a buffer to contain the received message.
// This buffer will be reused for each receive operation.
const int maxMessageSize = 1024;
byte[] receiveBuffer = new byte[maxMessageSize];
WebSocket socket = wsContext.WebSocket;
// While the WebSocket connection remains open we run a simple loop that receives messages and then sends them back.
while (socket.State == WebSocketState.Open)
{
// The first step is to begin a receive operation on the WebSocket. `ReceiveAsync` takes two parameters:
//
// * An `ArraySegment` to write the received data to. In this particular case we are passing an `ArraySegment` that points to our entire receive buffer.
// You might be wondering what the point of `ArraySegment` is and why `ReceiveAsync` doesn't just accept a `byte[]`. This will be explained below.
// * A cancellation token. In this example we are not using any timeouts so we use `CancellationToken.None`.
//
// `ReceiveAsync` returns a `Task<WebSocketReceiveResult>`. We use the await keyword to "asynchronously wait" for the receive operation to complete and
// extract the WebSocketReceiveResult from the completed task. The `WebSocketReceiveResult` provides information on the receive operation that was just
// completed, such as:
//
// * `WebSocketReceiveResult.MessageType` - What type of data was received and written to the provided buffer. Was it binary, utf8, or a close message?
// * `WebSocketReceiveResult.Count` - How many bytes were read?
// * `WebSocketReceiveResult.EndOfMessage` - Have we finished reading the data for this message or is there more coming?
WebSocketReceiveResult receiveResult = await socket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
// The WebSocket protocol defines a close handshake that allows a party to send a close frame when they wish to gracefully shut down the connection.
// The party on the other end can complete the close handshake by sending back a close frame.
//
// If we received a close frame then lets participate in the handshake by sending a close frame back. This is acheived by calling `CloseAsync`.
// `CloseAsync` will also terminate the underlying TCP connection once the close handshake is complete.
//
// The WebSocket protocol defines different status codes that can be sent as part of a close frame and also allows a close message to be sent.
// If we are just responding to the client's request to close we can just use `WebSocketCloseStatus.NormalClosure` and omit the close message.
if (receiveResult.MessageType == WebSocketMessageType.Close)
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
// This echo server can't handle binary messages so if we receive one we close the connection with an appropriate status code and message.
else if (receiveResult.MessageType == WebSocketMessageType.Binary)
{
await socket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "Cannot accept binary frame", CancellationToken.None);
}
else
{
// At this point we know we are receiving UTF-8 data, but we don't know if we've received the entire message or if we need to call `ReceiveAsync`
// again. We can use the `EndOfMessage` property to determine this and start a loop that will continue to call `ReceiveAsync` until we have
// read the entire message or exceeded our maximum message size (in which case we close the connection using an appropriate status code).
// The local `count` variable is used to keep track of the total number of bytes read for this message.
int count = receiveResult.Count;
while (receiveResult.EndOfMessage == false)
{
if (count >= maxMessageSize)
{
string closeMessage = string.Format("Maximum message size: {0} bytes.", maxMessageSize);
await socket.CloseAsync(WebSocketCloseStatus.MessageTooBig, closeMessage, CancellationToken.None);
return;
}
// Our first call to `ReceiveAsync` didn't return the data for the complete message so we are calling it again. Now the benefits of using
// `ArraySegment` over `byte[]` are clear - we can pass an `ArraySegment` that exposes the next section of our receive buffer by using the
// count of bytes that we have already read as an offset.
receiveResult = await socket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer, count, maxMessageSize - count), CancellationToken.None);
count += receiveResult.Count;
}
//### Decoding the message and sending a response
// The complete message is now in the receive buffer so we must convert the data to a string by decoding it as UTF-8. We know that we can do this
// because the WebSocket protocol only defines two different data formats - binary and UTF-8. If we wanted our server to receive text with
// a different encoding then we would expect to receive binary messages instead.
// After appending some text to the front of the message, we UTF-8 encode the string so that can get an `ArraySegment` with the appropriate data to send.
var receivedString = Encoding.UTF8.GetString(receiveBuffer, 0, count);
var echoString = "You said " + receivedString;
var json = new JavaScriptSerializer().Serialize(Get());
ArraySegment<byte> outputBuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(json));
// Now send the data using `SendAsync` using `WebSocketMessageType.Text` as the message type.
await socket.SendAsync(outputBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
// The echo operation is complete. The loop will resume and `ReceiveAsync` is called again to wait for the next message.
//
// You might be wondering about exception handling - for example, what if the client aborts the connection after sending a message but before receiving the echo?
// Won't an exception be thrown when `SendAsync` is called? Yes, but ASP.NET will catch the exception that bubbles up from our handler, dispose of the WebSocket
// for us and log the error in the Application event log. Of course a real application will certainly need error handling but its unnecessary for this simple example.
}
}
}
According to your description, I suggest you could check you have install the websocket firstly.
You could check it by using below steps:
1.Open Server Manager.
2.Under the Manage menu, click Add Roles and Features.
3.Select Role-based or Feature-based Installation, and then click Next.
4.Select the appropriate server, (your local server is selected by default), and then click Next.
5.Expand Web Server (IIS) in the Roles tree, then expand Web Server, and then expand Application Development.
6.Select WebSocket Protocol, and then click Next.
7.If no additional features are needed, click Next.
8.Click Install.
If you have already install the web socket, I suggest you could open the below path to find out the logs and post more details error information which related with the socket connection.
%SystemDrive%\inetpub\logs\LogFiles
Best Regards,
Brando
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
175 Points
608 Posts
Error when I try to connect to web socket
May 08, 2019 04:24 AM|neoaguil17|LINK
I created a web socket which works fine in IIS EXPRESS in my local host envirnoment, but i fails when I publish it in IIS..
I doesn't generate any error in the server..
I use Web socket test client from google
the url is: ws://centenario.somee.com/api/Chat/GetTesting?param=111
My code is:
I receive error...
Star
9591 Points
3012 Posts
Re: Error when I try to connect to web socket
May 08, 2019 09:22 AM|Brando ZWZ|LINK
Hi neoaguil17,
According to your description, I suggest you could check you have install the websocket firstly.
You could check it by using below steps:
1.Open Server Manager.
2.Under the Manage menu, click Add Roles and Features.
3.Select Role-based or Feature-based Installation, and then click Next.
4.Select the appropriate server, (your local server is selected by default), and then click Next.
5.Expand Web Server (IIS) in the Roles tree, then expand Web Server, and then expand Application Development.
6.Select WebSocket Protocol, and then click Next.
7.If no additional features are needed, click Next.
8.Click Install.
If you have already install the web socket, I suggest you could open the below path to find out the logs and post more details error information which related with the socket connection.
Best Regards,
Brando
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.