Skip to content

Commit 93feba9

Browse files
authored
added examples for RoomCallback usage (#599)
also changed: - moved data defs to data.go - added a few additional DisconnectReason
1 parent c476bac commit 93feba9

File tree

4 files changed

+108
-44
lines changed

4 files changed

+108
-44
lines changed

callback.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,34 @@ func (cb *ParticipantCallback) Merge(other *ParticipantCallback) {
125125
type DisconnectionReason string
126126

127127
const (
128-
LeaveRequested DisconnectionReason = "leave requested by room"
129-
UserUnavailable DisconnectionReason = "remote user unavailable"
130-
RejectedByUser DisconnectionReason = "rejected by remote user"
131-
Failed DisconnectionReason = "connection to room failed"
128+
LeaveRequested DisconnectionReason = "leave requested by user"
129+
UserUnavailable DisconnectionReason = "remote user unavailable"
130+
RejectedByUser DisconnectionReason = "rejected by remote user"
131+
Failed DisconnectionReason = "connection to room failed"
132+
RoomClosed DisconnectionReason = "room closed"
133+
ParticipantRemoved DisconnectionReason = "removed by server"
134+
DuplicateIdentity DisconnectionReason = "duplicate identity"
135+
OtherReason DisconnectionReason = "other reasons"
132136
)
133137

134138
func GetDisconnectionReason(reason livekit.DisconnectReason) DisconnectionReason {
135139
// TODO: SDK should forward the original reason and provide helpers like IsRequestedLeave.
136-
r := LeaveRequested
140+
r := OtherReason
137141
switch reason {
142+
case livekit.DisconnectReason_CLIENT_INITIATED:
143+
r = LeaveRequested
138144
case livekit.DisconnectReason_USER_UNAVAILABLE:
139145
r = UserUnavailable
140146
case livekit.DisconnectReason_USER_REJECTED:
141147
r = RejectedByUser
148+
case livekit.DisconnectReason_ROOM_CLOSED:
149+
r = RoomClosed
150+
case livekit.DisconnectReason_PARTICIPANT_REMOVED:
151+
r = ParticipantRemoved
152+
case livekit.DisconnectReason_DUPLICATE_IDENTITY:
153+
r = DuplicateIdentity
154+
case livekit.DisconnectReason_JOIN_FAILURE, livekit.DisconnectReason_SIGNAL_CLOSE, livekit.DisconnectReason_STATE_MISMATCH:
155+
r = Failed
142156
}
143157
return r
144158
}

callback_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lksdk_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/livekit/protocol/livekit"
7+
lksdk "github.com/livekit/server-sdk-go/v2"
8+
)
9+
10+
// ExampleRoomCallback demonstrates usage of RoomCallback to handle various room events
11+
func ExampleRoomCallback() {
12+
// Create a new callback handler
13+
cb := lksdk.NewRoomCallback()
14+
15+
// Handle data packets received from other participants
16+
cb.OnDataPacket = func(data lksdk.DataPacket, params lksdk.DataReceiveParams) {
17+
// handle DTMF
18+
switch val := data.(type) {
19+
case *livekit.SipDTMF:
20+
fmt.Printf("Received DTMF from %s: %s (%d)\n", params.SenderIdentity, val.Digit, val.Code)
21+
case *lksdk.UserDataPacket:
22+
fmt.Printf("Received user data from %s: %s\n", params.SenderIdentity, string(val.Payload))
23+
}
24+
}
25+
26+
// Handle participant metadata changes
27+
cb.OnAttributesChanged = func(changed map[string]string, p lksdk.Participant) {
28+
fmt.Printf("Participant %s attributes changed: %v\n", p.Identity(), changed)
29+
}
30+
31+
// Handle when current participant becomes disconnected
32+
cb.OnDisconnectedWithReason = func(reason lksdk.DisconnectionReason) {
33+
fmt.Printf("Disconnected from room: %s\n", reason)
34+
}
35+
36+
// Create a new room with the callback
37+
room := lksdk.NewRoom(cb)
38+
room.JoinWithToken("wss://myproject.livekit.cloud", "my-token")
39+
}

data.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
package lksdk
22

3-
type dataPublishOptions struct {
4-
Reliable *bool
5-
DestinationIdentities []string
6-
Topic string
3+
import (
4+
"github.com/livekit/protocol/livekit"
5+
"google.golang.org/protobuf/proto"
6+
)
7+
8+
// Data types
9+
10+
type DataPacket interface {
11+
ToProto() *livekit.DataPacket
12+
}
13+
14+
// Compile-time assertion for all supported data packet types.
15+
var (
16+
_ DataPacket = (*UserDataPacket)(nil)
17+
_ DataPacket = (*livekit.SipDTMF)(nil) // implemented in the protocol package
18+
)
19+
20+
// UserData is a custom user data that can be sent via WebRTC.
21+
func UserData(data []byte) *UserDataPacket {
22+
return &UserDataPacket{Payload: data}
23+
}
24+
25+
// UserDataPacket is a custom user data that can be sent via WebRTC on a custom topic.
26+
type UserDataPacket struct {
27+
Payload []byte
28+
Topic string // optional
729
}
830

31+
// ToProto implements DataPacket.
32+
func (p *UserDataPacket) ToProto() *livekit.DataPacket {
33+
var topic *string
34+
if p.Topic != "" {
35+
topic = proto.String(p.Topic)
36+
}
37+
return &livekit.DataPacket{Value: &livekit.DataPacket_User{
38+
User: &livekit.UserPacket{
39+
Payload: p.Payload,
40+
Topic: topic,
41+
},
42+
}}
43+
}
44+
45+
// receiving
46+
947
type DataReceiveParams struct {
1048
Sender *RemoteParticipant
1149
SenderIdentity string
1250
Topic string // Deprecated: Use UserDataPacket.Topic
1351
}
1452

53+
// publishing
54+
55+
type dataPublishOptions struct {
56+
Reliable *bool
57+
DestinationIdentities []string
58+
Topic string
59+
}
60+
1561
type DataPublishOption func(*dataPublishOptions)
1662

1763
func WithDataPublishTopic(topic string) DataPublishOption {

localparticipant.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -342,41 +342,6 @@ func (p *LocalParticipant) PublishData(payload []byte, opts ...DataPublishOption
342342
return p.PublishDataPacket(UserData(payload), opts...)
343343
}
344344

345-
type DataPacket interface {
346-
ToProto() *livekit.DataPacket
347-
}
348-
349-
// Compile-time assertion for all supported data packet types.
350-
var (
351-
_ DataPacket = (*UserDataPacket)(nil)
352-
_ DataPacket = (*livekit.SipDTMF)(nil) // implemented in the protocol package
353-
)
354-
355-
// UserData is a custom user data that can be sent via WebRTC.
356-
func UserData(data []byte) *UserDataPacket {
357-
return &UserDataPacket{Payload: data}
358-
}
359-
360-
// UserDataPacket is a custom user data that can be sent via WebRTC on a custom topic.
361-
type UserDataPacket struct {
362-
Payload []byte
363-
Topic string // optional
364-
}
365-
366-
// ToProto implements DataPacket.
367-
func (p *UserDataPacket) ToProto() *livekit.DataPacket {
368-
var topic *string
369-
if p.Topic != "" {
370-
topic = proto.String(p.Topic)
371-
}
372-
return &livekit.DataPacket{Value: &livekit.DataPacket_User{
373-
User: &livekit.UserPacket{
374-
Payload: p.Payload,
375-
Topic: topic,
376-
},
377-
}}
378-
}
379-
380345
// PublishDataPacket sends a packet via a WebRTC data channel. UserData can be used for sending custom user data.
381346
//
382347
// By default, the message can be received by all participants in a room,

0 commit comments

Comments
 (0)