/
WebSocketProtocol.cs
42 lines (36 loc) · 1.35 KB
/
WebSocketProtocol.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Threading;
namespace System.Net.WebSockets
{
public static class WebSocketProtocol
{
public static WebSocket CreateFromStream(
Stream stream,
bool isServer,
string? subProtocol,
TimeSpan keepAliveInterval)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!stream.CanRead || !stream.CanWrite)
{
throw new ArgumentException(!stream.CanRead ? SR.NotReadableStream : SR.NotWriteableStream, nameof(stream));
}
if (subProtocol != null)
{
WebSocketValidate.ValidateSubprotocol(subProtocol);
}
if (keepAliveInterval != Timeout.InfiniteTimeSpan && keepAliveInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(keepAliveInterval), keepAliveInterval,
SR.Format(SR.net_WebSockets_ArgumentOutOfRange_TooSmall,
0));
}
return ManagedWebSocket.CreateFromConnectedStream(stream, isServer, subProtocol, keepAliveInterval);
}
}
}