Hi everyone!
I created a small and easy-to-use C++ library to implement WebRTC in Defold.
For now it only works on the Web platform, but since it uses paullouisageneau’s libdatachannel it is highly cross-platform and should work on desktop and mobile with only some minor adjustments, I’m currently having problems compiling the libraries (especially OpenSSL for native platforms).
I tried to make it simple to use, taking inspiration from the websocket extension, here’s a simple test:
-- Called as soon as a signaling data it's created. (eg. offer, answer, candidates)
function on_signaling_data(self, data)
server.send(data)
end
function on_channel_info(self, event. id, label)
if event == "opened" then
print("Data channel \'" .. label .. "\' with " .. id .. " created!")
webrtc.send_message(id, label, "Hello!")
elseif event == "closed" then
print("Data channel \'" .. label .. "\' with " .. id .. " closed!")
end
end
function on_channel_message(self, id, label, msg)
print("[" .. id .. "] Got message from \'" .. label .. "\':" .. msg)
end
-- We're assuming the server only sends signaling data.
function on_server_message(self, message)
webrtc.process_data(message)
end
function init()
webrtc.set_configuration("stun_url:PORT;turn_url:PORT:USER:PASSWORD", on_signaling_data, on_channel_info, on_channel_message)
-- If we're the offerer, create a channel with another peer.
webrtc.create_channel(peer_id, "my_channel_name", webrtc.TYPE_RELIABLE)
end
For now, it has only some basic features:
- Creating channels with other peers using integer ids
- Adding a list of STUN and TURN servers to use
- Choosing the type of the channel, it can be RELIABLE, UNRELIABLE, and UNRELIABLE_ORDERED
- Sending simple text messages
There isn’t a websocket backend, the developer is free to decide to use whatever method they want for the signaling part (just in case there will be an example that uses the Defold websocket extension).
The project is still in a heavy development phase so it isn’t very stable but it does work: It’s already able to create a connection between two peers behind NATs using a TURN server.
It’s also my first-ever extension for Defold so let me know any feedback you may have and I’ll be more than happy to add it to the project!