2

I want to send an UDP package to my UDP server in browser. I heard that HTML5 or WEBRTC can do something, but I don't know how.

Can someone help me?

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
user6043069
  • 31
  • 1
  • 3
  • are you talking about sending message using [RTCDataChannel](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel)? – mido Mar 23 '16 at 09:22
  • as long as i can send a udp(i know the data format) to my udp server using browser,thats OK – user6043069 Mar 24 '16 at 06:56
  • Or is it a duplicate of https://stackoverflow.com/questions/13216785/how-to-send-a-udp-packet-with-web-rtc-javascript/13478490 ? – Martin Thomson Sep 07 '17 at 13:23

1 Answers1

3

You could either write a chrome extension (app) which would give you access to https://developer.chrome.com/apps/sockets_udp ("sockets": {...} in your manifest.json).

Or, as far as WebRTC goes:

var pc = new webkitRTCPeerConnection(
    { "iceServers": [{ "url": "stun:localhost:1234" }] }
);

pc.createOffer(function (sessionDescription) {
    pc.setLocalDescription(sessionDescription);

}, function(error) {
    alert(error);
}, { 'mandatory': { 'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true } });

You'd then able to get the UDP packets on localhost:1234 via:

$port = 1234

t = Thread.start do
  server = UDPSocket.open
  server.bind(nil, $port)
  a = server.recvfrom(12364)
  puts server.send "ping", 0, a[1][2], a[1][1]
end
t.join
wpp
  • 7,093
  • 4
  • 33
  • 65
  • I know the data format,How can i send my data,for example ,my data is [0x80,0x01,0x11]. – user6043069 Mar 25 '16 at 07:47
  • I'd look into building a chrome extension then: https://developer.chrome.com/apps/sockets_udp#method-send. – wpp Mar 25 '16 at 11:56
  • Apparently UDP sockets are not possible in extensions anymore. It was possible at one point, but they disabled it due to security concerns -- restricting it only to Chrome apps, which are being ended/phased-out now as well. :/ – Venryx Jul 06 '19 at 04:29
  • I was able to get some packages using this `webkitRTCPeerConnection` API, but they seem to be encrypted or something. How do I actually parse it / read it? Also, how do I send my data, like some text for example? – Jerry Green Apr 04 '21 at 12:38