diff --git a/app/assets/javascripts/signaling-server.js b/app/assets/javascripts/signaling-server.js index d807c8b..25db06a 100644 --- a/app/assets/javascripts/signaling-server.js +++ b/app/assets/javascripts/signaling-server.js @@ -17,6 +17,7 @@ let localstream; window.onload = () => { currentUser = document.getElementById("current-user").innerHTML; + console.log(currentUser) localVideo = document.getElementById("local-video"); remoteVideoContainer = document.getElementById("remote-video-container"); }; @@ -41,21 +42,48 @@ document.onreadystatechange = async () => { }; // find chatroom +// const handleJoinSession = async () => { +// App.session = await App.cable.subscriptions.create("VideoSessionChannel", { +// connected: () => { +// broadcastData({ +// type: JOIN_ROOM, +// from: currentUser +// }); +// }, +// received: data => { +// console.log("received", data); +// if (data.from === currentUser) return; +// switch (data.type) { +// case JOIN_ROOM: +// return joinRoom(data); +// case EXCHANGE: +// if (data.to !== currentUser) return; +// return exchange(data); +// case REMOVE_USER: +// return removeUser(data); +// default: +// return; +// } +// } +// }); +// }; const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"] const handleJoinSession = async () => { - App.session = await App.cable.subscriptions.create({ + App['chatroom' + chatroomId] = await App.cable.subscriptions.create({ channel: "ChatRoomsChannel", room: chatroomId }, { connected: () => { + console.log(chatroomId) broadcastData({ type: JOIN_ROOM, - from: currentUser + from: currentUser, + room: chatroomId }); }, received: data => { - console.log("received", data); + console.log(data) if (data.from === currentUser) return; switch (data.type) { case JOIN_ROOM: @@ -93,7 +121,6 @@ const joinRoom = data => { }; const removeUser = data => { - console.log("removing user", data.from); let video = document.getElementById(`remoteVideoContainer+${data.from}`); video && video.remove(); delete pcPeers[data.from]; @@ -101,18 +128,21 @@ const removeUser = data => { const broadcastData = data => { - fetch("sessions", { + if (data.type === EXCHANGE) { + console.log("yayyy") + } + fetch("chat_room_sessions", { method: "POST", body: JSON.stringify(data), - headers: { "content-type": "application/json" } + headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content } }); }; const createPC = (userId, isOffer) => { let pc = new RTCPeerConnection(ice); + let test = userId pcPeers[userId] = pc; pc.addStream(localstream); - if (isOffer) { pc .createOffer() @@ -122,7 +152,8 @@ const createPC = (userId, isOffer) => { type: EXCHANGE, from: currentUser, to: userId, - sdp: JSON.stringify(pc.localDescription) + sdp: JSON.stringify(pc.localDescription), + room: chatroomId }); }) .catch(logError); @@ -134,7 +165,8 @@ const createPC = (userId, isOffer) => { type: EXCHANGE, from: currentUser, to: userId, - candidate: JSON.stringify(event.candidate) + candidate: JSON.stringify(event.candidate), + room: chatroomId }); } }; @@ -191,7 +223,8 @@ const exchange = data => { type: EXCHANGE, from: currentUser, to: data.from, - sdp: JSON.stringify(pc.localDescription) + sdp: JSON.stringify(pc.localDescription), + room: chatroomId }); }); } diff --git a/app/controllers/chat_rooms_controller.rb b/app/controllers/chat_rooms_controller.rb index fb9e3cd..37b681c 100644 --- a/app/controllers/chat_rooms_controller.rb +++ b/app/controllers/chat_rooms_controller.rb @@ -5,6 +5,20 @@ class ChatRoomsController < ApplicationController end def create + # HTTP status code 200 with an empty body + head :no_content + puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>11123213213213123213" + puts params + puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>11123213213213123213" + ActionCable.server.broadcast "chat_room_#{params[:room]}", session_params + end + + private + + def session_params + # SDP = Session description protocol (codec info from client) + # Candidate = ICE candidates (e.g. TURN and STUN server) + params.permit(:type, :from, :to, :sdp, :candidate, :room) end end diff --git a/app/javascript/packs/chatrooms.js b/app/javascript/packs/chatrooms.js index bd474f1..46b193b 100644 --- a/app/javascript/packs/chatrooms.js +++ b/app/javascript/packs/chatrooms.js @@ -17,10 +17,8 @@ App['chatroom' + chatroomId] = App.cable.subscriptions.create({ room: chatroomId }, { connected: () => { - console.log('Connected to ChatRoom ' + chatroomId) }, received: data => { - console.log(data) } }) diff --git a/app/views/chat_rooms/show.html.erb b/app/views/chat_rooms/show.html.erb index 4ad0142..4f6f6f4 100644 --- a/app/views/chat_rooms/show.html.erb +++ b/app/views/chat_rooms/show.html.erb @@ -1,6 +1,21 @@ + +