From c3b072ba383c65d6e7434617a9e6201ed0c9d109 Mon Sep 17 00:00:00 2001 From: Beatrice Olivera Date: Thu, 30 Aug 2018 18:42:42 +0100 Subject: [PATCH] added room id in exchange parameters --- app/assets/javascripts/signaling-server.js | 53 ++++++++++++++++++---- app/controllers/chat_rooms_controller.rb | 14 ++++++ app/javascript/packs/chatrooms.js | 2 - app/views/chat_rooms/show.html.erb | 15 ++++++ app/views/pages/call.html.erb | 4 +- config/routes.rb | 4 +- 6 files changed, 77 insertions(+), 15 deletions(-) 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 @@ + +

This is chatroom #<%= @chat_room.id %>

+
+
+
+ +
+
+
+ +
+ + +
+ <%= javascript_pack_tag 'chatrooms' %> diff --git a/app/views/pages/call.html.erb b/app/views/pages/call.html.erb index 700efaa..9776324 100644 --- a/app/views/pages/call.html.erb +++ b/app/views/pages/call.html.erb @@ -1,5 +1,5 @@ - + diff --git a/config/routes.rb b/config/routes.rb index 2f9eca8..a14a2d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - get 'video_sessions/create' + devise_for :users, path: '', path_names: { sign_out: 'logout'} devise_scope :user do get '/logout', to: 'devise/sessions#destroy' @@ -24,6 +24,8 @@ Rails.application.routes.draw do get '/contacts', to: 'pages#index' post '/sessions', to: 'video_sessions#create' + post '/chat_rooms/chat_room_sessions', to: 'chat_rooms#create' + resources :chat_rooms, only: [ :show ] do # testing action cable post '/cable_testing', to: 'pages#cable_testing'