MCPcopy Index your code
hub / github.com/TensorWorks/Unreal-PixelStreaming-RTP-Bridge / startControlLoop

Function startControlLoop

main.go:186–243  ·  view source on GitHub ↗

Starts an infinite loop where we poll for new websocket messages and react to them.

(wsConn *websocket.Conn, peerConnection *webrtc.PeerConnection, pendingCandidates *[]*webrtc.ICECandidate)

Source from the content-addressed store, hash-verified

184
185// Starts an infinite loop where we poll for new websocket messages and react to them.
186func startControlLoop(wsConn *websocket.Conn, peerConnection *webrtc.PeerConnection, pendingCandidates *[]*webrtc.ICECandidate) {
187 // Start loop here to read web socket messages
188 for {
189
190 messageType, message, err := wsConn.ReadMessage()
191 if err != nil {
192 log.Printf("Websocket read message error: %v", err)
193 log.Printf("Closing Pion websocket control loop.")
194 wsConn.Close()
195 break
196 }
197 stringMessage := string(message)
198
199 // We print the recieved messages in a different colour so they are easier to distinguish.
200 colorGreen := "\033[32m"
201 colorReset := "\033[0m"
202 fmt.Println(string(colorGreen), fmt.Sprintf("Received message, (type=%d): %s", messageType, stringMessage), string(colorReset))
203
204 // Transform the raw bytes into a map of string: []byte pairs, we can unmarshall each key/value as needed.
205 var objmap map[string]json.RawMessage
206 err = json.Unmarshal(message, &objmap)
207
208 if err != nil {
209 log.Printf("Error unmarshalling bytes from websocket message. Error: %s", err.Error())
210 continue
211 }
212
213 // Get the type of message we received from the Unreal Engine side
214 var pixelStreamingMessageType string
215 err = json.Unmarshal(objmap["type"], &pixelStreamingMessageType)
216
217 if err != nil {
218 log.Printf("Error unmarshaling type from pixel streaming message. Error: %s", err.Error())
219 continue
220 }
221
222 // Based on the "type" of message we received, we react accordingly.
223 switch pixelStreamingMessageType {
224 case "playerCount":
225 var playerCount int
226 err = json.Unmarshal(objmap["count"], &playerCount)
227 if err != nil {
228 log.Printf("Error unmarshaling player count. Error: %s", err.Error())
229 }
230 fmt.Println(fmt.Sprintf("Player count is: %d", playerCount))
231 case "config":
232 fmt.Println("Got config message, ToDO: react based on config that was passed.")
233 case "answer":
234 handleRemoteAnswer(message, peerConnection, wsConn, pendingCandidates)
235 case "iceCandidate":
236 candidateMsg := objmap["candidate"]
237 handleRemoteIceCandidate(candidateMsg, peerConnection)
238 default:
239 log.Println("Got message we do not specifically handle, type was: " + pixelStreamingMessageType)
240 }
241
242 }
243}

Callers 1

mainFunction · 0.85

Calls 2

handleRemoteAnswerFunction · 0.85
handleRemoteIceCandidateFunction · 0.85

Tested by

no test coverage detected