On this page, there are two parts that are essential for the voice chat application to work properly. the first is hosting the voice chat server. the second is adding the files in your unity game and setting up some variables.
First, import the asset to your game. This will add all the files needed to get you started.
After importing the asset, the main script that you need to learn about and use will go by the name "VoiceChatHandler.cs", this script is singelton, so you can use it directly in any class you make.
Tip: just make sure to add this script on a gameobject for the first scene, this is preferable to ensure running "SingletonAwakened" when your game start. this function initializes the voicechat script that will be loaded on the webpage that host the game.
the important thing that will need to change on the script editor will be the modification of two variables in the.
public string socketServer = "/";
public string clientJSFileURL = "/";
The first variable should contain the link to your website "https://your-server-domain", for it is used to communicate with the websocket server.
The second varialbe will be the link to the js file that contains the voice chat logic that is going to be imported into the webpage by unity. this file is important so that unity can start communicating with the server.
the link for the file should go like this (https://your-server-domain/js/client.js)
pre-requisites (for the hosting device): -
note: hosting the server on heroku provides an easier experince and the server comes with an ssl.
To begin with, the server can be found in "Assets/WebGLVoiceChat/VoiceChatServer.zip" from the imported asset, this zip includes the server that can be started using node js.
the link below shows how to host the server on heroku. However, you can host ther server on any platform of your choice. How to deploy to heroku
to run the server use
npm run prod
after setting up the server and hosting it, accessing the main page of the website will be empty but the server is being hosted.
you can also check the demo here.
starting from the update 0.7.0 the voice chat handler asset now uses two way communication. so the browser needs to have access to the unity instance.
in order to do this you need to modify the index.html file that comes with the webgl build where this line needs to be added
After adding the unity files to your game and providing the required URLs, you will have access to all the functionality of the voice chat library. However, none of them will be functional in the editor, for the functionality itself is executed on the browser with JavaScript.
the way it works is that when the game loads, the VCH (VoiceChatHandler) will instantiate itself (if used) and stay persistent throughout the game. On Start, the class will use the provided URLs from the class (in the editor) to do two things.
the first URL will be used as the domain to connect to the voice chat server, and the other URL will be the location of the voice chat script which will be used to append the script to the current HTML page by unity.
when the file loads, the class will be ready to be used.
Since update 0.8.0, there is an admin panel for the running server that can be accessed through "your-domain-name/Admin" e.g "localhost/Admin" the "A" in "Admin" is case sensitive.
once you enter the page, you should fill these three parameters
the first two should be somehting similar to the image above (the server url will change based on where it is hosted).
the password is defined from the server code, and should be chnaged from it
the image above showcases where the password can be changed. this code section can be found in the "index.js" file in the server zip.
the password should be encrypted with bcrypt and then inserted in the password property as in the picture above.
you can use a website like bcrypt-generator which will encrypt your password with the needed algorithm.
to learn more about the admin panel you can visit this link.
Welcome to the Docs of the Webgl Voice Chat Solution
this Documentation is meant to explain the mechanism of how does the software work, document it's functions and help you getting started.
to get started check the getting started page.
to learn more about the functions and functionality of the solution check the docs section
in simple terms, the voice chat is a separate application that runs on your browser. the twist is that scripts were written in unity, allowing communication between unity and the voice chat application itself (the js file from the server).
so unity communicates with it while the application handles rooms, and people, and fetches the voice from the microphone with transmission over the network, etc... .
the reason it was built this way is that the in-availability of access to the mic in unity for WebGL, alongside the limitation of only TCP/HTTP protocol on the browser prevents the development of a functional voice chat within the unity game on the browser and thus the current solution.
instantiate the class
var voiceChat = new VoiceChat();
Nan
contains the socket io connection of the current client. learn more here.
contains an instance of hark (used for speech detection). learn more here.
the mute status of the current client.
contains a list of the current room(s) the client is connected to.
contains a list of the other clients the current client has muted locally.
the interval time used between recordings to be sent through the network.
this is a temporary variable for storing the clients id of a requested specfic room and is only relevant when the function is called.
this will be set by exposing the unity instance after it's creation in the window variable to enable access in this class.
it is used to allow communtication from the browser to Unity game build.
this is the microphone manager component / class that will take care of managing the microphone stream and change of the microphone
this is the Chat manager component / class that will take care of sending / receiving messages for the room/s the player is in.
the below functions are internally used within the class itself and does not need to be called.
starts the voice stream and sends it to the server (only needs to run once, automatically runs with the constructor).
will take an array buffer (base64 encoded audio) and emit it to the socket server, the socket server has a list of the rooms the player is in.
this function takes a mediaStream source starts the recording loop and returns an ArrayBuffer.
this function takes a MediaStream and initializes hark with it alongside registering speech detection events.
the below functions are meant to be used externally by other scripts.
returns a boolean of whether the current client is speaking or not.
returns a string of the current client socket id
takes a boolean to mute/unmute the current client from others
takes a string of another client ID and checks it they are muted by the local client (local mute)
takes a string of another client ID and mutes them locally for the current client
takes a string of another client ID and unmutes them locally for the current client
unmutes them locally for the current client
returns a list of all the rooms the current user is currently at.
returns a list of all clients in the provided room.
takes a room id and create/join the current user to the room id provided.
takes a room id and disconnects the current user from the room id provided if they are in that room.
disconnects the current user from all the rooms they have joined.
this.MicrohphoneManager = new Microhphone(voiceChatInstance);
the constructor takes an instance of the voice chat class
this event will be triggerd whenever a stream is ready / changed and will send the stream as a parameter
a refrence of the voicechat instance
a refrence of the current localStream (microphone stream)
stores a list of the available devices of type "audioinput" so that the user/player can use it to switch / pick one to be used/changed with.
stores a refrence of whethere the player/user have access to the microphone
this function returns a promise with a boolean of whethere is the user is permmited to access the microphone.
returns a promise with a list of all the available audioinput devices
Promise<MediaDeviceInfo[]>
changes the currently used stream. takes the current stream if available and the new audioinput device id as params and closes the current stream tracks than replace the current stream with the new one by the provided device id.
fetches the current stream from the user using the default audioinput device set by the OS.
this.ChatManager = new ChatManager(voiceChatInstance);
the constructor takes an instance of the voice chat class
Nan
Nan
this funcition takes a string as a parameter for the message and emits it to the room/s the player is in.
this fucntion takes two parameters, the message and the client id whom sent the message then sends it back to the unity instance. the function will work whenever a new message recived from the another client.
this class is a singleton that can be used anywhere in a unity game project which handles communication with the voice chat instance of JavaScript in the browser, this is how the Singleton was achieved in unity with mono behavior functionality, here.
public string socketServer
This is going to be the variable that will hold the socket server URL which is going to be used in the build to connect the socket server
public string clientJSFileURL
the URL link to the client js file that contains the voice chat logic for the browser. This will be used to append the code from the URL to the current page the game is hosted on.
private List<string> clientsInCurrentRoom
holds the list of of clients in the current joined room. (will be updated automatically from the browser).
private List<Microphone> availableMicrophones
stores a list of the availabe microphones
private bool isSpeaking
an indicator of whether the client is speaking or not. (will be updated automatically from the browser).
public UnityEvent<List<string>> OnClientsInRoom
this event will be triggered when the browser sends back the list of clients in the room
Returns
List<string>
public UnityEvent<bool> onIsSpeaking
when invoked, returns a string of a JSON with the status of whether is the current client is talking or not.
tip: this value can be used with other networking framework solutions(mirror, fishnet, smartfox, etc...) to send the player status to other in-game players.
Returns
bool
public UnityEvent<List<Microphone>> OnAvailableMicrophones
this event is triggered when the browser sends back a list of available microphones (audioinput)
Returns
List<Microphone>
public UnityEvent<bool> OnCanAccessMicrophone
this event is triggered when the browser sends back a response for whethere if you the player/user have provided access to the voice chat script.
Returns
bool
public UnityEvent<string,string> OnMessageReceived
this event is triggered when the browser sends back a response with a message received. th first parameter represents the clientID who sent the message and the second parameter represents the actual message itself.
Returns
<string, string>
public string GetMySocketID()
this function returns a string of the current client socket-io id which can be used directly as a string
e.g: -
"w2YTHXzcN2fJCuROAAAB"
Returns
string
public string IsVoiceChatReady()
this function returns a string of whether the voice chat browser app is ready to be used or not
e.g: -
"yes"
or
"no"
Returns
string
public void MuteMic()
this function mutes your mic.
public void UnMuteMic()
this function unmutes your mic.
public bool IsMute()
this function returns a bool indicating whether if the mic is mute or not
Returns
bool
public void SetIsSpeaking(string isSpeaking)
sets the is speaking paramater and invokes the onIsSpeaking event. (the browser will update the list using the function)
public bool IsSpeaking()
this function returns bool of whether the current user is speaking or not regardless of whether the mic is mute or not
Returns
bool
public void GetAvailableMicrophones()
sends a request to the browser the fetch all the available microphones. the resoponse is sent back to "SetAvailableMicrophones()" function.
public void SetAvailableMicrophones(string microphonesJson)
this funcition is called by the browser which sends back the list of microphones fetched.
the function itself populates "availableMicrophones" variable and triggers the "OnAvailableMicrophones" event, sending the list to whatever function is listening to it.
public void SetCurrentMicrophone(Microphone microphone)
this function sends a request to the browser to change the currently used microphone from the browser. it takes a "VoiceChatSolution.Microphone" struct as a parameter.
public void CanAccessMicrophone()
sends a request to the browser about whether the browser has access to the microphone or not. the resoponse is set back in "CanAccessMicrophoneResponse" function.
public void CanAccessMicrophoneResponse(string canAccess)
this function is called by the browser where it is provided with an object that holds a boolean of whether the browser has access or not.
this function also triggers "OnCanAccessMicrophone" event sending the result back to whatever function is listening to it.
public List<string> GetRooms()
this function returns a list of strings of the rooms the player is currently joined in.
furthermore, it refreshes the list of rooms. the new list of rooms is then set by browser through "SetRooms" function.
Returns
List<string>
public void SetRooms(string roomsJson)
sets the list of the rooms the client is in.
public void UpdateClientsInRoom(string roomID)
the function runs a process where it fetches the clients in a specified room, than the browser will update the list using "SetClientsInRoom" function
public void SetClientsInRoom(string clients)
sets the list of clients in the current room the user/player is in. (the browser will excute this function)
the event "OnClientsInRoom" is triggered with list of the clients.
public void JoinRoom(string roomID)
the function joins/creates and joins the player in the voice chat room
public void JoinOneRoom(string roomID)
same as the one above but it makes sure that you leave rooms before joining the given one
public void LeaveRoom(string roomID)
disconnects the client from the room id given
public void LeaveAllRooms()
disconnects the client from all the rooms they are connected to.
public void MuteOtherClient(string clientID)
locally mutes another client for the current client.
public void UnMuteOtherClient(string clientID)
locally unmutes another client for the current client.
public void UnMuteAllClients()
locally unmutes all muted clients for the current client.
public bool IsOtherClientMuted(string clientID)
this function returns a bool indicating whether if the provided client is muted locally or not by the current user.
Returns
bool
public List<string> GetMuteList()
returns a list of strings of all the locally muted other clients
Returns
List<string>
public void SendChatMessage(string message)
takes a string as the message and sends it to the browser to be sent to other clients of the same room.
public void ReceiveChatMessage(string jsonMessage)
this function is it triggerd by the browser which sends any message that is intended for the current client / player.
this function also triggers the "OnMessageReceived" event sending the data to whatever function is listening to it.
public struct Microphone
{
public string deviceId;
public string kind;
public string label;
public string groupId;
}
this structure is used to allocate the microphone meta data within the VoiceChatHandler Script.