Skip to content

Commit 4ef3732

Browse files
committed
accumulate websocket chunks in lws
1 parent 4d91e8e commit 4ef3732

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

examples/libpeer/src/libpeer_connection.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,16 @@ void WebrtcConnection::sendSignalingMessage(
119119

120120
void WebrtcConnection::handleMessage(nrtc::util::WebrtcSignalingMessage& msg) {
121121
std::lock_guard<std::mutex> lock(mutex_);
122+
122123
if (msg.isDescription()) {
123124
auto desc = msg.getDescription();
124-
NPLOGI << "Description received: " << desc.sdp;
125+
auto type = desc.type == "offer" ? SDP_TYPE_OFFER : SDP_TYPE_ANSWER;
126+
peer_connection_set_remote_description(pc_, desc.sdp.c_str(), type);
127+
}
128+
129+
if (msg.isCandidate()) {
130+
std::string cand = msg.getCandidate().candidate;
131+
peer_connection_add_ice_candidate(pc_, cand.data());
125132
}
126133
}
127134

@@ -139,6 +146,16 @@ void WebrtcConnection::onIceConnectionStateChange(PeerConnectionState state,
139146
NPLOGI << "ICE Connection state changed to " << state;
140147
}
141148

142-
void WebrtcConnection::onIceCandidate(char* description, void* userdata) {}
149+
void WebrtcConnection::onIceCandidate(char* description, void* userdata) {
150+
WebrtcConnection* conn = reinterpret_cast<WebrtcConnection*>(userdata);
151+
std::string cand = std::string(description);
152+
nrtc::util::SignalingCandidate signalingCandidate(cand);
153+
154+
// @TODO: Probably not correct to just set "0" here.
155+
signalingCandidate.setSdpMid("0");
156+
157+
conn->sendSignalingMessage(
158+
nrtc::util::WebrtcSignalingMessage(signalingCandidate));
159+
}
143160

144-
} // namespace nabto::example
161+
} // namespace nabto::example

examples/libpeer/src/lws_websocket.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,14 @@ int LwsWebsocket::lwsCallback(struct lws* wsi, enum lws_callback_reasons reason,
114114
}
115115

116116
case LWS_CALLBACK_CLIENT_RECEIVE: {
117-
std::string message(static_cast<const char*>(in), len);
118-
NPLOGI << message;
119117
std::lock_guard<std::mutex> lock(ws->callbackMutex_);
120-
if (ws->onMessage_) {
121-
ws->onMessage_(message);
118+
ws->receiveBuffer_.append(static_cast<const char*>(in), len);
119+
120+
if (lws_is_final_fragment(wsi)) {
121+
if (ws->onMessage_) {
122+
ws->onMessage_(ws->receiveBuffer_);
123+
}
124+
ws->receiveBuffer_.clear();
122125
}
123126
break;
124127
}

examples/libpeer/src/lws_websocket.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LwsWebsocket : public nabto::webrtc::SignalingWebsocket {
4242
std::shared_ptr<LwsContextManager> contextManager_ =
4343
LwsContextManager::getInstance();
4444
struct lws* wsi_ = nullptr;
45+
std::string receiveBuffer_;
4546
std::atomic<bool> connected_{false};
4647

4748
// Callbacks

0 commit comments

Comments
 (0)