@@ -15,6 +15,12 @@ import (
15
15
// ChatRoomBufSize is the number of incoming messages to buffer for each topic.
16
16
const ChatRoomBufSize = 128
17
17
18
+ // Topic used to broadcast browser WebRTC addresses
19
+ const PubSubDiscoveryTopic string = "universal-connectivity-browser-peer-discovery"
20
+
21
+ const ChatTopic string = "universal-connectivity"
22
+ const ChatFileTopic string = "universal-connectivity-file"
23
+
18
24
// ChatRoom represents a subscription to a single PubSub topic. Messages
19
25
// can be published to the topic with ChatRoom.Publish, and received
20
26
// messages are pushed to the Messages channel.
@@ -23,13 +29,15 @@ type ChatRoom struct {
23
29
Messages chan * ChatMessage
24
30
SysMessages chan * ChatMessage
25
31
26
- ctx context.Context
27
- h host.Host
28
- ps * pubsub.PubSub
29
- chatTopic * pubsub.Topic
30
- chatSub * pubsub.Subscription
31
- fileTopic * pubsub.Topic
32
- fileSub * pubsub.Subscription
32
+ ctx context.Context
33
+ h host.Host
34
+ ps * pubsub.PubSub
35
+ chatTopic * pubsub.Topic
36
+ chatSub * pubsub.Subscription
37
+ fileTopic * pubsub.Topic
38
+ fileSub * pubsub.Subscription
39
+ peerDiscoveryTopic * pubsub.Topic
40
+ peerDiscoverySub * pubsub.Subscription
33
41
34
42
roomName string
35
43
nick string
@@ -44,9 +52,9 @@ type ChatMessage struct {
44
52
45
53
// JoinChatRoom tries to subscribe to the PubSub topic for the room name, returning
46
54
// a ChatRoom on success.
47
- func JoinChatRoom (ctx context.Context , h host.Host , ps * pubsub.PubSub , nickname string , roomName string ) (* ChatRoom , error ) {
55
+ func JoinChatRoom (ctx context.Context , h host.Host , ps * pubsub.PubSub , nickname string ) (* ChatRoom , error ) {
48
56
// join the pubsub chatTopic
49
- chatTopic , err := ps .Join (chatTopicName ( roomName ) )
57
+ chatTopic , err := ps .Join (ChatTopic )
50
58
if err != nil {
51
59
return nil , err
52
60
}
@@ -58,7 +66,7 @@ func JoinChatRoom(ctx context.Context, h host.Host, ps *pubsub.PubSub, nickname
58
66
}
59
67
60
68
// join the pubsub fileTopic
61
- fileTopic , err := ps .Join (fileTopicName ( roomName ) )
69
+ fileTopic , err := ps .Join (ChatFileTopic )
62
70
if err != nil {
63
71
return nil , err
64
72
}
@@ -69,18 +77,31 @@ func JoinChatRoom(ctx context.Context, h host.Host, ps *pubsub.PubSub, nickname
69
77
return nil , err
70
78
}
71
79
80
+ // join the pubsub peer disovery topic
81
+ peerDiscoveryTopic , err := ps .Join (PubSubDiscoveryTopic )
82
+ if err != nil {
83
+ return nil , err
84
+ }
85
+
86
+ // and subscribe to it
87
+ peerDiscoverySub , err := peerDiscoveryTopic .Subscribe ()
88
+ if err != nil {
89
+ return nil , err
90
+ }
91
+
72
92
cr := & ChatRoom {
73
- ctx : ctx ,
74
- h : h ,
75
- ps : ps ,
76
- chatTopic : chatTopic ,
77
- chatSub : chatSub ,
78
- fileTopic : fileTopic ,
79
- fileSub : fileSub ,
80
- nick : nickname ,
81
- roomName : roomName ,
82
- Messages : make (chan * ChatMessage , ChatRoomBufSize ),
83
- SysMessages : make (chan * ChatMessage , ChatRoomBufSize ),
93
+ ctx : ctx ,
94
+ h : h ,
95
+ ps : ps ,
96
+ chatTopic : chatTopic ,
97
+ chatSub : chatSub ,
98
+ fileTopic : fileTopic ,
99
+ fileSub : fileSub ,
100
+ peerDiscoveryTopic : peerDiscoveryTopic ,
101
+ peerDiscoverySub : peerDiscoverySub ,
102
+ nick : nickname ,
103
+ Messages : make (chan * ChatMessage , ChatRoomBufSize ),
104
+ SysMessages : make (chan * ChatMessage , ChatRoomBufSize ),
84
105
}
85
106
86
107
// start reading messages from the subscription in a loop
@@ -94,7 +115,7 @@ func (cr *ChatRoom) Publish(message string) error {
94
115
}
95
116
96
117
func (cr * ChatRoom ) ListPeers () []peer.ID {
97
- return cr .ps .ListPeers (chatTopicName ( cr . roomName ) )
118
+ return cr .ps .ListPeers (ChatTopic )
98
119
}
99
120
100
121
// readLoop pulls messages from the pubsub chat/file topic and handles them.
@@ -187,13 +208,3 @@ func (cr *ChatRoom) requestFile(toPeer peer.ID, fileID []byte) ([]byte, error) {
187
208
188
209
return fileBody , nil
189
210
}
190
-
191
- // chatTopicName returns the name of the pubsub topic for the chat room.
192
- func chatTopicName (roomName string ) string {
193
- return roomName
194
- }
195
-
196
- // fileTopicName returns the name of the pubsub topic used for sending/recieving files in the chat room.
197
- func fileTopicName (roomName string ) string {
198
- return fmt .Sprintf ("%s-file" , roomName )
199
- }
0 commit comments