Table of Contents

Class Utils

Namespace
WitShells.WebRTCWit
public static class Utils
Inheritance
Utils
Inherited Members

Methods

AttachCode(SignalMessage, string)

Attach a matchmaking code to a message (stored in sessionId).

public static void AttachCode(SignalMessage msg, string code)

Parameters

msg SignalMessage
code string

BuildWireSignal(SignalMessage, string)

Build the wire format for signaling used by this project: "WIT_RTC_SIGNAL|{scope}|{json}".

public static string BuildWireSignal(SignalMessage msg, string scope)

Parameters

msg SignalMessage
scope string

Returns

string

BuildWireSignalWithCode(SignalMessage, string, string)

Build a wire signal including scope and code for easy routing. Wire: WIT_RTC_SIGNAL|{scope}|{code}|{json}

public static string BuildWireSignalWithCode(SignalMessage msg, string scope, string code)

Parameters

msg SignalMessage
scope string
code string

Returns

string

CreateDataChannelInit(bool)

Create a DataChannel init object for reliable/unreliable channels.

public static Unity.WebRTC.RTCDataChannelInit CreateDataChannelInit(bool reliable)

Parameters

reliable bool

Returns

RTCDataChannelInit

CreateIceSignal(string, string, int, string, string)

Create a SignalMessage for an ICE candidate.

public static SignalMessage CreateIceSignal(string candidate, string sdpMid, int sdpMLineIndex, string fromId = null, string toId = null)

Parameters

candidate string
sdpMid string
sdpMLineIndex int
fromId string
toId string

Returns

SignalMessage

CreateRtcConfig(string[], string[], string, string)

Create an RTCConfiguration with optional STUN and TURN servers. Example: new[] { "stun:stun.l.google.com:19302" }

public static Unity.WebRTC.RTCConfiguration CreateRtcConfig(string[] stunUrls = null, string[] turnUrls = null, string turnUsername = null, string turnCredential = null)

Parameters

stunUrls string[]
turnUrls string[]
turnUsername string
turnCredential string

Returns

RTCConfiguration

CreateSdpSignal(string, string, string, string)

Create a SignalMessage for an SDP offer/answer.

public static SignalMessage CreateSdpSignal(string sdp, string type, string fromId = null, string toId = null)

Parameters

sdp string
type string
fromId string
toId string

Returns

SignalMessage

DeserializeSignal(string)

Deserialize a SignalMessage from JSON. Returns null if parse fails.

public static SignalMessage DeserializeSignal(string json)

Parameters

json string

Returns

SignalMessage

GenerateCode(int)

Generate a short alphanumeric matchmaking code (e.g., 6 chars).

public static string GenerateCode(int length = 6)

Parameters

length int

Returns

string

MatchesCode(SignalMessage, string)

Check if a message matches the provided code.

public static bool MatchesCode(SignalMessage msg, string code)

Parameters

msg SignalMessage
code string

Returns

bool

RegisterDataChannelEvents(RTCDataChannel, UnityAction<RTCDataChannel>, UnityAction<RTCDataChannel>, UnityAction<RTCDataChannel, byte[]>)

public static void RegisterDataChannelEvents(RTCDataChannel channel, UnityAction<Unity.WebRTC.RTCDataChannel> onOpen = null, UnityAction<Unity.WebRTC.RTCDataChannel> onClose = null, UnityAction<Unity.WebRTC.RTCDataChannel, byte[]> onMessage = null)

Parameters

channel RTCDataChannel
onOpen UnityAction<RTCDataChannel>
onClose UnityAction<RTCDataChannel>
onMessage UnityAction<RTCDataChannel, byte[]>

SerializeSignal(SignalMessage)

Serialize a SignalMessage to JSON.

public static string SerializeSignal(SignalMessage msg)

Parameters

msg SignalMessage

Returns

string

SetLocalDescriptionCoroutine(RTCPeerConnection, RTCSessionDescription, Action<string>)

Coroutine helper to set local description on a peer connection and yield until complete. On error, optional onError is invoked with message.

public static IEnumerator SetLocalDescriptionCoroutine(Unity.WebRTC.RTCPeerConnection pc, Unity.WebRTC.RTCSessionDescription desc, Action<string> onError = null)

Parameters

pc RTCPeerConnection
desc RTCSessionDescription
onError Action<string>

Returns

IEnumerator

SetRemoteDescriptionCoroutine(RTCPeerConnection, RTCSessionDescription, Action<string>)

Coroutine helper to set remote description on a peer connection and yield until complete.

public static IEnumerator SetRemoteDescriptionCoroutine(Unity.WebRTC.RTCPeerConnection pc, Unity.WebRTC.RTCSessionDescription desc, Action<string> onError = null)

Parameters

pc RTCPeerConnection
desc RTCSessionDescription
onError Action<string>

Returns

IEnumerator

TryParseWireSignal(string, out string, out SignalMessage)

Try to parse the wire format built with BuildWireSignal.

public static bool TryParseWireSignal(string wire, out string scope, out SignalMessage msg)

Parameters

wire string
scope string
msg SignalMessage

Returns

bool

TryParseWireSignalWithCode(string, out string, out string, out SignalMessage)

Try parse wire signal with scope and code.

public static bool TryParseWireSignalWithCode(string wire, out string scope, out string code, out SignalMessage msg)

Parameters

wire string
scope string
code string
msg SignalMessage

Returns

bool