C# Class Luz.Net.WebRtcNetwork

Uses an underlaying java script library to give network access in browsers. Use WebRtcNetwork.IsAvailable() first to check if it can run. If the java script part of the library is included + the browser supports WebRtc it should return true. If the java script part of the library is not included you can inject it at runtime by using WebRtcNetwork.InjectJsCode(). It is recommecend to include the js files though. To allow incomming connections use StartServer() or StartServer("my room name") To connect oothers use Connect("room name"); To send messages use SendData. You will need to handle incomming events by polling the Dequeue method.
Inheritance: IBasicNetwork
Show file Open project: devluz/webrtcnetwork Class Usage Examples

Public Methods

Method Description
Connect ( string name ) : ConnectionId

Connects to the given name or address.

Dequeue ( NetworkEvent &evt ) : bool

Dequeues a new event

Disconnect ( ConnectionId id ) : void

Disconnects the given connection id.

Dispose ( ) : void

Disposes the underlaying java script library. If you have long running systems that don't reuse instances make sure you always call dispose as unity doesn't seem to call destructors reliably. You might fill up your java script memory with lots of unused instances.

Flush ( ) : void

Flushes messages. Not needed in WebRtcNetwork but use it at the end of a frame if you want to be able to replace WebRtcNetwork with other implementations

InjectJsCode ( ) : void

This injects the library using ExternalEval. Browsers seem to load some libraries asynchronly though. This means that directly after InjectJsCode some things aren't available yet. So starting a server/connecting won't work directly after this call yet. Add at least 1-2 seconds waiting time for the browser to download the libraries or better -> include everything needed into the websites header so this call isn't needed at all!

IsAvailable ( ) : bool

Will return true if the environment supports the WebRTCNetwork plugin (needs to run in Chrome or Firefox + the javascript file needs to be loaded in the html page!) Keep in mind that it doesn't check if libraries needed for signaling channels are loaded! e.g. it returns true even though firebase or socket.io are missing. Make sure the webpage is setup properly including those in the header tag!

SendData ( ConnectionId conId, byte data, int offset, int length, bool reliable ) : void

Sends a byte array

Shutdown ( ) : void

Shuts webrtc down. All connection will be disconnected + if the server is started it will be stopped. The instance itself isn't released yet! Use Dispose to destroy the network entirely.

StartServer ( ) : void

Starts a server using a random number as address/name. Read the ServerInitialized events Info property to get the address name.

StartServer ( string name ) : void

Allows to listen to incomming connections using a given name/address. This is in addition to the definition of the IBaseNetwork interface which is shared with other network systems enforcing the use of ip:port as address, thus can't allow custom addresses.

Update ( ) : void

Needs to be called to read data from the unterlaying network and update this class. Use Dequeue to get the events it read.

WebRtcNetwork ( ) : System

Creates a WebRtcNetwork using default values. This can be used for testing but as it uses an unreliable test server you should setup the WebRtcNetwork(config) for anything you want to release.

WebRtcNetwork ( string config ) : System

Creats a new network by using a JSON configuration string. This is used to configure the server connection for the signaling channel and to define webrtc specific configurations such as stun server used to connect through firewalls. might look like this: string config = "{ \"Signaling\" : { \"name\" : \"FirebaseSignalingChan\", \"conf\" : \"https://incandescent-inferno-5269.firebaseio.com/webrtcnetwork0_9/\"}}"; or string config = "{ \"Signaling\" : { \"name\" : \"SocketIoSignalingChan\", \"conf\" : \"https://4science.azurewebsites.net:443\"},{\"iceServers\" : [ {\"url\":\"stun:stun.l.google.com:19302\"}]}}"; Make sure all json " signs are escaped. The JSON object will be parsed in java script and then given to the library. So their content is dependend on the underlaying library. Check out the webrtcnetwork.js to understand the details.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Private Methods

Method Description
DequeueInternal ( NetworkEvent &evt ) : bool

Retrieves an event from the js library, handles it internallay and then adds it to a queue for delivery to the user.

HandleEventInternally ( NetworkEvent &evt ) : void

Handles events internally. Needed to change the internal states: Server flag and connection id list. Would be better to remove that in the future from the main library and treat it separately.

UnityWebRtcNetworkConnect ( int lIndex, string lRoom ) : int
UnityWebRtcNetworkCreate ( string lConfiguration ) : int
UnityWebRtcNetworkDequeue ( int lIndex, int lTypeIntArrayPtr, int lConidIntArrayPtr, byte lUint8ArrayDataPtr, int lUint8ArrayDataOffset, int lUint8ArrayDataLength, int lDataLenIntArray ) : bool
UnityWebRtcNetworkDisconnect ( int lIndex, int lConnectionId ) : void
UnityWebRtcNetworkIsAvailable ( ) : bool
UnityWebRtcNetworkPeekEventDataLength ( int lIndex ) : int
UnityWebRtcNetworkRelease ( int lIndex ) : void
UnityWebRtcNetworkSendData ( int lIndex, int lConnectionId, byte lUint8ArrayDataPtr, int lUint8ArrayDataOffset, int lUint8ArrayDataLength, bool lReliable ) : void
UnityWebRtcNetworkShutdown ( int lIndex ) : void
UnityWebRtcNetworkStartServer ( int lIndex, string lRoom ) : void

Method Details

Connect() public method

Connects to the given name or address.
public Connect ( string name ) : ConnectionId
name string The address identifying the server
return Luz.ULib.Net.ConnectionId

Dequeue() public method

Dequeues a new event
public Dequeue ( NetworkEvent &evt ) : bool
evt Luz.ULib.Net.NetworkEvent
return bool

Disconnect() public method

Disconnects the given connection id.
public Disconnect ( ConnectionId id ) : void
id Luz.ULib.Net.ConnectionId Id to disconnect
return void

Dispose() public method

Disposes the underlaying java script library. If you have long running systems that don't reuse instances make sure you always call dispose as unity doesn't seem to call destructors reliably. You might fill up your java script memory with lots of unused instances.
public Dispose ( ) : void
return void

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void

Flush() public method

Flushes messages. Not needed in WebRtcNetwork but use it at the end of a frame if you want to be able to replace WebRtcNetwork with other implementations
public Flush ( ) : void
return void

InjectJsCode() public static method

This injects the library using ExternalEval. Browsers seem to load some libraries asynchronly though. This means that directly after InjectJsCode some things aren't available yet. So starting a server/connecting won't work directly after this call yet. Add at least 1-2 seconds waiting time for the browser to download the libraries or better -> include everything needed into the websites header so this call isn't needed at all!
public static InjectJsCode ( ) : void
return void

IsAvailable() public static method

Will return true if the environment supports the WebRTCNetwork plugin (needs to run in Chrome or Firefox + the javascript file needs to be loaded in the html page!) Keep in mind that it doesn't check if libraries needed for signaling channels are loaded! e.g. it returns true even though firebase or socket.io are missing. Make sure the webpage is setup properly including those in the header tag!
public static IsAvailable ( ) : bool
return bool

SendData() public method

Sends a byte array
public SendData ( ConnectionId conId, byte data, int offset, int length, bool reliable ) : void
conId Luz.ULib.Net.ConnectionId Connection id the message should be delivered to.
data byte Content/Buffer that contains the content
offset int Start index of the content in data
length int Length of the content in data
reliable bool True to use the ordered, reliable trainfer, false for unordered and unreliable
return void

Shutdown() public method

Shuts webrtc down. All connection will be disconnected + if the server is started it will be stopped. The instance itself isn't released yet! Use Dispose to destroy the network entirely.
public Shutdown ( ) : void
return void

StartServer() public method

Starts a server using a random number as address/name. Read the ServerInitialized events Info property to get the address name.
public StartServer ( ) : void
return void

StartServer() public method

Allows to listen to incomming connections using a given name/address. This is in addition to the definition of the IBaseNetwork interface which is shared with other network systems enforcing the use of ip:port as address, thus can't allow custom addresses.
public StartServer ( string name ) : void
name string Name/Address can be any kind of string. There might be restrictions though depending /// on the underlaying signaling channel. /// An invalid name will result in an InitFailed event being return in Dequeue.
return void

Update() public method

Needs to be called to read data from the unterlaying network and update this class. Use Dequeue to get the events it read.
public Update ( ) : void
return void

WebRtcNetwork() public method

Creates a WebRtcNetwork using default values. This can be used for testing but as it uses an unreliable test server you should setup the WebRtcNetwork(config) for anything you want to release.
public WebRtcNetwork ( ) : System
return System

WebRtcNetwork() public method

Creats a new network by using a JSON configuration string. This is used to configure the server connection for the signaling channel and to define webrtc specific configurations such as stun server used to connect through firewalls. might look like this: string config = "{ \"Signaling\" : { \"name\" : \"FirebaseSignalingChan\", \"conf\" : \"https://incandescent-inferno-5269.firebaseio.com/webrtcnetwork0_9/\"}}"; or string config = "{ \"Signaling\" : { \"name\" : \"SocketIoSignalingChan\", \"conf\" : \"https://4science.azurewebsites.net:443\"},{\"iceServers\" : [ {\"url\":\"stun:stun.l.google.com:19302\"}]}}"; Make sure all json " signs are escaped. The JSON object will be parsed in java script and then given to the library. So their content is dependend on the underlaying library. Check out the webrtcnetwork.js to understand the details.
public WebRtcNetwork ( string config ) : System
config string
return System