A constructor function that generates and returns a new PubSub for a given P2PHost, username and roomname
(p2phost *P2P, clientName string, topicName string)
| 63 | // A constructor function that generates and returns a new |
| 64 | // PubSub for a given P2PHost, username and roomname |
| 65 | func JoinPubSub(p2phost *P2P, clientName string, topicName string) (*PubSub, error) { |
| 66 | |
| 67 | // Create a PubSub topic with the room name |
| 68 | topic, err := p2phost.PubSub.Join(fmt.Sprintf("icefiredb-sqlite-pub-sub-p2p-%s", topicName)) |
| 69 | // Check the error |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | |
| 74 | // Subscribe to the PubSub topic |
| 75 | sub, err := topic.Subscribe() |
| 76 | // Check the error |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | |
| 81 | // Check the provided clientname |
| 82 | if clientName == "" { |
| 83 | // Use the default client name |
| 84 | clientName = defaultclient |
| 85 | } |
| 86 | |
| 87 | // Check the provided topicname |
| 88 | if topicName == "" { |
| 89 | // Use the default topic name |
| 90 | topicName = defaulttopic |
| 91 | } |
| 92 | |
| 93 | // Create cancellable context |
| 94 | pubsubctx, cancel := context.WithCancel(context.Background()) |
| 95 | |
| 96 | // Create a PubSub object |
| 97 | PubSub := &PubSub{ |
| 98 | Host: p2phost, |
| 99 | |
| 100 | Inbound: make(chan chatmessage), |
| 101 | Outbound: make(chan string), |
| 102 | Logs: make(chan chatlog), |
| 103 | |
| 104 | psctx: pubsubctx, |
| 105 | pscancel: cancel, |
| 106 | pstopic: topic, |
| 107 | psub: sub, |
| 108 | |
| 109 | ClientName: clientName, |
| 110 | TopicName: topicName, |
| 111 | selfid: p2phost.Host.ID(), |
| 112 | } |
| 113 | |
| 114 | // Start the subscribe loop |
| 115 | go PubSub.SubLoop() |
| 116 | // Start the publish loop |
| 117 | go PubSub.PubLoop() |
| 118 | |
| 119 | // Return the PubSub |
| 120 | return PubSub, nil |
| 121 | } |
| 122 |
no test coverage detected