Skip to content

Added Options for WebSocket. #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public class Connection : MonoBehaviour
// Start is called before the first frame update
async void Start()
{
//WebSocketOptions example.
//WebSocketOptions options = new WebSocketOptions();
//options.url = "ws://localhost:3000";
//options.keepAliveInterval = TimeSpan.FromSeconds(10); //Ping Interval.
//headers...
//subprotocols...
//Credentials...

//websocket = new WebSocket(options);

// websocket = new WebSocket("ws://echo.websocket.org");
websocket = new WebSocket("ws://localhost:3000");

Expand Down
123 changes: 47 additions & 76 deletions NativeWebSocket/Assets/WebSocket/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ public ConfiguredTaskAwaitable.ConfiguredTaskAwaiter GetAwaiter()
}
}

public class WebSocketOptions
{
public string url = "ws://localhost:3000";
public Dictionary<string, string> headers = new Dictionary<string, string>();
public List<string> subprotocols = new List<string>();
public TimeSpan keepAliveInterval = TimeSpan.FromSeconds(30); //Ping interval
//Credentials...
}

#if UNITY_WEBGL && !UNITY_EDITOR

/// <summary>
Expand Down Expand Up @@ -229,43 +238,35 @@ public class WebSocket : IWebSocket {
public event WebSocketErrorEventHandler OnError;
public event WebSocketCloseEventHandler OnClose;

public WebSocket (string url, Dictionary<string, string> headers = null) {
if (!WebSocketFactory.isInitialized) {
WebSocketFactory.Initialize ();
}

int instanceId = WebSocketFactory.WebSocketAllocate (url);
WebSocketFactory.instances.Add (instanceId, this);

this.instanceId = instanceId;
public WebSocket(string url)
{
WebSocketOptions options = new WebSocketOptions();
options.url = url;
setWebSocketOptions(options);
}

public WebSocket (string url, string subprotocol, Dictionary<string, string> headers = null) {
if (!WebSocketFactory.isInitialized) {
WebSocketFactory.Initialize ();
}

int instanceId = WebSocketFactory.WebSocketAllocate (url);
WebSocketFactory.instances.Add (instanceId, this);

WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol);

this.instanceId = instanceId;
public WebSocket(WebSocketOptions options)
{
setWebSocketOptions(options);
}

public WebSocket (string url, List<string> subprotocols, Dictionary<string, string> headers = null) {
if (!WebSocketFactory.isInitialized) {
public void setWebSocketOptions(WebSocketOptions options)
{
if (!WebSocketFactory.isInitialized) {
WebSocketFactory.Initialize ();
}
}

int instanceId = WebSocketFactory.WebSocketAllocate (url);
WebSocketFactory.instances.Add (instanceId, this);
int instanceId = WebSocketFactory.WebSocketAllocate (options.url);
WebSocketFactory.instances.Add (instanceId, this);

foreach (string subprotocol in subprotocols) {
WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol);
}
foreach (string subprotocol in options.subprotocols) {
WebSocketFactory.WebSocketAddSubProtocol(instanceId, subprotocol);
}

this.instanceId = instanceId;
//headers?????
//creditentials...

this.instanceId = instanceId;
}

~WebSocket () {
Expand Down Expand Up @@ -373,6 +374,8 @@ public class WebSocket : IWebSocket
private Uri uri;
private Dictionary<string, string> headers;
private List<string> subprotocols;
private TimeSpan keepAliveInterval;
//Credentials...
private ClientWebSocket m_Socket = new ClientWebSocket();

private CancellationTokenSource m_TokenSource;
Expand All @@ -385,60 +388,25 @@ public class WebSocket : IWebSocket
private List<ArraySegment<byte>> sendBytesQueue = new List<ArraySegment<byte>>();
private List<ArraySegment<byte>> sendTextQueue = new List<ArraySegment<byte>>();

public WebSocket(string url, Dictionary<string, string> headers = null)
public WebSocket(string url)
{
uri = new Uri(url);

if (headers == null)
{
this.headers = new Dictionary<string, string>();
}
else
{
this.headers = headers;
}

subprotocols = new List<string>();

string protocol = uri.Scheme;
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
throw new ArgumentException("Unsupported protocol: " + protocol);
WebSocketOptions options = new WebSocketOptions();
options.url = url;
setWebSocketOptions(options);
}

public WebSocket(string url, string subprotocol, Dictionary<string, string> headers = null)
public WebSocket(WebSocketOptions options)
{
uri = new Uri(url);

if (headers == null)
{
this.headers = new Dictionary<string, string>();
}
else
{
this.headers = headers;
}

subprotocols = new List<string> {subprotocol};

string protocol = uri.Scheme;
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
throw new ArgumentException("Unsupported protocol: " + protocol);
setWebSocketOptions(options);
}

public WebSocket(string url, List<string> subprotocols, Dictionary<string, string> headers = null)
public void setWebSocketOptions(WebSocketOptions options)
{
uri = new Uri(url);

if (headers == null)
{
this.headers = new Dictionary<string, string>();
}
else
{
this.headers = headers;
}

this.subprotocols = subprotocols;
uri = new Uri(options.url);
headers = options.headers;
subprotocols = options.subprotocols;
keepAliveInterval = options.keepAliveInterval;
//Credentials...

string protocol = uri.Scheme;
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
Expand Down Expand Up @@ -468,6 +436,9 @@ public async Task Connect()
m_Socket.Options.AddSubProtocol(subprotocol);
}

m_Socket.Options.KeepAliveInterval = keepAliveInterval;
//m_Socket.Options.Credentials...

await m_Socket.ConnectAsync(uri, m_CancellationToken);
OnOpen?.Invoke();

Expand Down