diff --git a/pom.xml b/pom.xml index 80ceaf5ca0..cfb3fe1889 100644 --- a/pom.xml +++ b/pom.xml @@ -635,6 +635,7 @@ spring-boot-nashorn java-blockchain machine-learning + webrtc wildfly quarkus-extension @@ -883,7 +884,6 @@ spring-boot-nashorn java-blockchain - diff --git a/webrtc/pom.xml b/webrtc/pom.xml new file mode 100644 index 0000000000..2a3b48b2ed --- /dev/null +++ b/webrtc/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + webrtc + 0.0.1 + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-websocket + + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/webrtc/src/main/java/com/baeldung/webrtc/SocketHandler.java b/webrtc/src/main/java/com/baeldung/webrtc/SocketHandler.java new file mode 100644 index 0000000000..bffd57f6cf --- /dev/null +++ b/webrtc/src/main/java/com/baeldung/webrtc/SocketHandler.java @@ -0,0 +1,31 @@ +package com.baeldung.webrtc; + +import org.springframework.stereotype.Component; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +@Component +public class SocketHandler extends TextWebSocketHandler { + + List sessions = new CopyOnWriteArrayList<>(); + + @Override + public void handleTextMessage(WebSocketSession session, TextMessage message) throws InterruptedException, IOException { + + for (WebSocketSession webSocketSession : sessions) { + if (webSocketSession.isOpen() && !session.getId().equals(webSocketSession.getId())) { + webSocketSession.sendMessage(message); + } + } + } + + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + sessions.add(session); + } +} \ No newline at end of file diff --git a/webrtc/src/main/java/com/baeldung/webrtc/WebRTCDemoApplication.java b/webrtc/src/main/java/com/baeldung/webrtc/WebRTCDemoApplication.java new file mode 100644 index 0000000000..f54946547d --- /dev/null +++ b/webrtc/src/main/java/com/baeldung/webrtc/WebRTCDemoApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.webrtc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WebRTCDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(WebRTCDemoApplication.class, args); + } +} diff --git a/webrtc/src/main/java/com/baeldung/webrtc/WebSocketConfiguration.java b/webrtc/src/main/java/com/baeldung/webrtc/WebSocketConfiguration.java new file mode 100644 index 0000000000..9deb395dad --- /dev/null +++ b/webrtc/src/main/java/com/baeldung/webrtc/WebSocketConfiguration.java @@ -0,0 +1,17 @@ +package com.baeldung.webrtc; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; + +@Configuration +@EnableWebSocket +public class WebSocketConfiguration implements WebSocketConfigurer { + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + registry.addHandler(new SocketHandler(), "/socket") + .setAllowedOrigins("*"); + } +} diff --git a/webrtc/src/main/resources/application.properties b/webrtc/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/webrtc/src/main/resources/static/client.js b/webrtc/src/main/resources/static/client.js new file mode 100644 index 0000000000..059dc84bcb --- /dev/null +++ b/webrtc/src/main/resources/static/client.js @@ -0,0 +1,116 @@ +//connecting to our signaling server +var conn = new WebSocket('ws://localhost:8080/socket'); + +conn.onopen = function() { + console.log("Connected to the signaling server"); + initialize(); +}; + +conn.onmessage = function(msg) { + console.log("Got message", msg.data); + var content = JSON.parse(msg.data); + var data = content.data; + switch (content.event) { + // when somebody wants to call us + case "offer": + handleOffer(data); + break; + case "answer": + handleAnswer(data); + break; + // when a remote peer sends an ice candidate to us + case "candidate": + handleCandidate(data); + break; + default: + break; + } +}; + +function send(message) { + conn.send(JSON.stringify(message)); +} + +var peerConnection; +var dataChannel; +var input = document.getElementById("messageInput"); + +function initialize() { + var configuration = null; + + peerConnection = new RTCPeerConnection(configuration, { + optional : [ { + RtpDataChannels : true + } ] + }); + + // Setup ice handling + peerConnection.onicecandidate = function(event) { + if (event.candidate) { + send({ + event : "candidate", + data : event.candidate + }); + } + }; + + // creating data channel + dataChannel = peerConnection.createDataChannel("dataChannel", { + reliable : true + }); + + dataChannel.onerror = function(error) { + console.log("Error occured on datachannel:", error); + }; + + // when we receive a message from the other peer, printing it on the console + dataChannel.onmessage = function(event) { + console.log("message:", event.data); + }; + + dataChannel.onclose = function() { + console.log("data channel is closed"); + }; +} + +function createOffer() { + peerConnection.createOffer(function(offer) { + send({ + event : "offer", + data : offer + }); + peerConnection.setLocalDescription(offer); + }, function(error) { + alert("Error creating an offer"); + }); +} + +function handleOffer(offer) { + peerConnection.setRemoteDescription(new RTCSessionDescription(offer)); + + // create and send an answer to an offer + peerConnection.createAnswer(function(answer) { + peerConnection.setLocalDescription(answer); + send({ + event : "answer", + data : answer + }); + }, function(error) { + alert("Error creating an answer"); + }); + +}; + +function handleCandidate(candidate) { + peerConnection.addIceCandidate(new RTCIceCandidate(candidate)); +}; + +function handleAnswer(answer) { + peerConnection.setRemoteDescription(new RTCSessionDescription(answer)); + console.log("connection established successfully!!"); +}; + +function sendMessage() { + dataChannel.send(input.value); + input.value = ""; +} diff --git a/webrtc/src/main/resources/static/index.html b/webrtc/src/main/resources/static/index.html new file mode 100644 index 0000000000..39a86ec61e --- /dev/null +++ b/webrtc/src/main/resources/static/index.html @@ -0,0 +1,67 @@ + + + +WebRTC demo + + + + + + + + + + +
+

A Demo for messaging in WebRTC

+ +

+ Run two instances of this webpage along with the server to test this + application.
Create an offer, and then send the message.
Check + the browser console to see the output. +

+ + + + + + + + +
+ + + + +