@@ -125,6 +125,10 @@ void WebsocketService::OnHandshake(beast::error_code ec) {
125125void WebsocketService::Read () {
126126 std::visit (
127127 [this ](auto &ws) {
128+ if (!ws.is_open ()) {
129+ return ;
130+ }
131+
128132 ws.async_read (buffer_,
129133 [this ](boost::system::error_code ec, std::size_t bytes_transferred) {
130134 if (ec) {
@@ -141,51 +145,63 @@ void WebsocketService::Read() {
141145}
142146
143147void WebsocketService::OnMessage (const std::string &req) {
148+ DEBUG_PRINT (" Received message: %s" , req.c_str ());
149+ try {
150+ nlohmann::json jsonObj = nlohmann::json::parse (req.c_str ());
151+ } catch (const std::exception &e) {
152+ ERROR_PRINT (" Failed to parse message: %s" , e.what ());
153+ return ;
154+ }
155+
144156 json jsonObj = json::parse (req.c_str ());
145157 std::string action = jsonObj[" action" ];
146158 std::string message = jsonObj[" message" ];
147- DEBUG_PRINT (" Received message: %s" , req.c_str ());
148159
149160 if (action == " join" ) {
150161 PeerConfig config;
151- webrtc::PeerConnectionInterface::IceServer ice_server ;
162+ config. is_sfu_peer = true ;
152163
164+ webrtc::PeerConnectionInterface::IceServer ice_server;
153165 nlohmann::json messageJson = nlohmann::json::parse (jsonObj[" message" ].get <std::string>());
154166 ice_server.urls = messageJson[" urls" ].get <std::vector<std::string>>();
155167 ice_server.username = messageJson[" username" ];
156168 ice_server.password = messageJson[" credential" ];
157169 config.servers .push_back (ice_server);
158170
159- pub_peer_ = conductor->CreatePeerConnection (config);
160- pub_peer_->OnLocalSdp (
161- [this ](const std::string &peer_id, const std::string &sdp, const std::string &type) {
171+ pub_peer_ = CreatePeer (config);
172+ if (pub_peer_) {
173+ pub_peer_->OnLocalSdp ([this ](const std::string &peer_id, const std::string &sdp,
174+ const std::string &type) {
162175 Write (type, sdp);
163176 });
164- pub_peer_->OnLocalIce ([this ](const std::string &peer_id, const std::string &sdp_mid,
165- int sdp_mline_index, const std::string &candidate) {
166- Write (" trickle" , candidate);
167- });
177+ pub_peer_->OnLocalIce ([this ](const std::string &peer_id, const std::string &sdp_mid,
178+ int sdp_mline_index, const std::string &candidate) {
179+ Write (" trickle" , candidate);
180+ });
181+ }
168182
169183 config.is_publisher = false ;
170- sub_peer_ = conductor->CreatePeerConnection (config);
171- sub_peer_->OnLocalSdp (
172- [this ](const std::string &peer_id, const std::string &sdp, const std::string &type) {
184+ sub_peer_ = CreatePeer (config);
185+ if (sub_peer_) {
186+ sub_peer_->OnLocalSdp ([this ](const std::string &peer_id, const std::string &sdp,
187+ const std::string &type) {
173188 Write (type, sdp);
174189 });
175- sub_peer_->OnLocalIce ([this ](const std::string &peer_id, const std::string &sdp_mid,
176- int sdp_mline_index, const std::string &candidate) {
177- Write (" trickle" , candidate);
178- });
190+ sub_peer_->OnLocalIce ([this ](const std::string &peer_id, const std::string &sdp_mid,
191+ int sdp_mline_index, const std::string &candidate) {
192+ Write (" trickle" , candidate);
193+ });
194+ }
179195
180196 Write (" addVideoTrack" , args_.uid );
181197 if (!args_.no_audio ) {
182198 Write (" addAudioTrack" , args_.uid );
183199 }
184- } else if (action == " offer" && !sub_peer_->IsConnected ()) {
200+ } else if (action == " offer" && sub_peer_ && !sub_peer_->IsConnected ()) {
185201 sub_peer_->SetRemoteSdp (message, " offer" );
186- } else if (action == " answer" && !pub_peer_->IsConnected ()) {
202+ } else if (action == " answer" && pub_peer_ && !pub_peer_->IsConnected ()) {
187203 pub_peer_->SetRemoteSdp (message, " answer" );
188- } else if (action == " trackPublished" ) {
204+ } else if (action == " trackPublished" && pub_peer_ ) {
189205 pub_peer_->CreateOffer ();
190206 } else if (action == " trickle" ) {
191207 OnRemoteIce (message);
0 commit comments