Chat using WebRTC
What for?
P2P(Peer to Peer) is a means of communication with the lowest possible cost.
This module provides P2P text/voice chat for Native(C#) and WebGL(JavaScript).
Specification
- P2P text chat is available.
- P2P voice chat is available.
Architecture
Unity
JavaScript
Installation
Package
Unity
https://github.com/extreal-dev/Extreal.Integration.Chat.WebRTC.git
npm
@extreal-dev/extreal.integration.chat.webrtc
Dependencies
This module uses the following packages.
Unity
- Extreal.Core.Logging
- Extreal.Core.Common
- Extreal.Integration.Web.Common
- Extreal.Integration.P2P.WebRTC
- WebRTC
- UniTask
- UniRx
npm
Please refer to Release for the correspondence between module version and each package version.
Settings
This module uses P2P.WebRTC to realize P2P. WebRTC Settings is required. Please add the following initialization after setting up P2P.WebRTC.
Create a Client with each Provider.
public class ClientControlScope : LifetimeScope
{
protected override void Configure(IContainerBuilder builder)
{
var peerConfig = new PeerConfig("http://127.0.0.1:3010");
var peerClient = PeerClientProvider.Provide(peerConfig);
builder.RegisterComponent(peerClient);
var textChatClient = TextChatClientProvider.Provide(peerClient);
builder.RegisterComponent(textChatClient);
var voiceChatClient = VoiceChatClientProvider.Provide(peerClient);
builder.RegisterComponent(voiceChatClient);
builder.RegisterEntryPoint<ClientControlPresenter>();
}
}
If used with WebGL, further JavaScript initialization is required. Create each Adapter and call the adapt function.
import { PeerAdapter } from "@extreal-dev/extreal.integration.p2p.webrtc";
import { TextChatAdapter, VoiceChatAdapter } from "@extreal-dev/extreal.integration.chat.webrtc";
const peerAdapter = new PeerAdapter();
peerAdapter.adapt();
const textChatAdapter = new TextChatAdapter();
textChatAdapter.adapt(peerAdapter.getPeerClient);
const voiceChatAdapter = new VoiceChatAdapter();
voiceChatAdapter.adapt(peerAdapter.getPeerClient);
Usage
Perform P2P text chat
Text chat uses P2P.WebRTC to realize P2P. Use P2P.WebRTC's API to establish P2P connections.
Text chat features are provided by TextChatClient.
Use the Send method to send messages.
textChatClient.Send(message);
The OnMessageReceived event is used to receive a message. A string message is passed as a parameter.
textChatClient.OnMessageReceived
.Subscribe(message =>
{
// do something
})
.AddTo(disposables);
To end the text chat, use the Clear method.
textChatClient.Clear();
Perform P2P voice chat
Voice chat uses P2P.WebRTC to realize P2P. Please use P2P.WebRTC's API to establish P2P connections.
Voice Chat features are provided by VoiceChatClient.
Use the ToggleMute method to switch mute.
voiceChatClient.ToggleMute();
The value(bool) after switching to mute is received in the OnMuted event.
voiceChatClient.OnMuted
.Subscribe(muted =>
{
// do something
})
.AddTo(disposables);
If you want to specify the default value for mute, specify it in VoiceChatConfig.
var voiceChatConfig = new VoiceChatConfig(initialMute: false);
var voiceChatClient = VoiceChatClientProvider.Provide(peerClient, voiceChatConfig);