Skip to content

Commit

Permalink
fix: fixing use of new client port
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen authored Dec 5, 2023
1 parent 7c66df5 commit 3fdfb1b
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions Assets/SimpleWebSocket/Runtime/WebSocketFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using JamesFrowen.SimpleWeb;
using Mirage.SocketLayer;
using UnityEngine;
using System.ComponentModel;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace JamesFrowen.Mirage.Sockets.SimpleWeb
{
Expand Down Expand Up @@ -67,7 +71,7 @@ private void Awake()
public override ISocket CreateClientSocket()
{
// todo get max message size somewhere else?
bool useWss = sslEnabled || clientUseWss;
var useWss = sslEnabled || clientUseWss;
return new ClientWebSocket(tcpConfig, MaxPacketSize, useWss);
}

Expand All @@ -79,7 +83,7 @@ public override ISocket CreateServerSocket()
}

// todo get max message size somewhere else?
SslConfig sslConfig = SslConfigLoader.Load(sslEnabled, sslCertJson, sslProtocols);
var sslConfig = SslConfigLoader.Load(sslEnabled, sslCertJson, sslProtocols);
return new ServerWebSocket(tcpConfig, MaxPacketSize, sslConfig);
}

Expand All @@ -100,6 +104,10 @@ public override IEndPoint GetConnectEndPoint(string address = null, ushort? port
{
builder.Port = (int)port;
}
else
{
builder.Port = ClientPort.GetPort(this);
}

return new SimpleWebEndPoint(builder.Uri);
}
Expand All @@ -111,7 +119,27 @@ public override IEndPoint GetConnectEndPoint(string address = null, ushort? port
public struct ClientPortSettings
{
public ClientPortOptions Option;
public ushort CustomPort;
public int CustomPort;

public int GetPort(WebSocketFactory factory)
{
return GetPort(Option, CustomPort, factory);
}

public static int GetPort(ClientPortOptions option, int customPort, WebSocketFactory factory)
{
switch (option)
{
case ClientPortOptions.SameAsServer:
return factory.ServerPort;
case ClientPortOptions.HttpDefault:
return factory.clientUseWss ? 443 : 80;
case ClientPortOptions.Custom:
return customPort;
default:
throw new InvalidEnumArgumentException(nameof(option), (int)option, typeof(ClientPortOptions));
}
}
}
public enum ClientPortOptions
{
Expand All @@ -120,11 +148,9 @@ public enum ClientPortOptions
Custom
}
}
#if UNITY_EDITOR
namespace Mirror.SimpleWeb.EditorScripts
{
#if UNITY_EDITOR
using UnityEditor;

[CustomPropertyDrawer(typeof(ClientPortSettings))]
public class ClientPortSettingsDrawer : PropertyDrawer
{
Expand Down Expand Up @@ -159,10 +185,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
var port = 0;
if (property.serializedObject.targetObject is WebSocketFactory swt)
{
if (option == ClientPortOptions.HttpDefault)
port = swt.clientUseWss ? 443 : 80;
else
port = swt.ServerPort;
port = ClientPortSettings.GetPort(option, 0, swt);
}

var wasEnabled = GUI.enabled;
Expand All @@ -176,5 +199,5 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
}
}
#endif
}
#endif

0 comments on commit 3fdfb1b

Please sign in to comment.