Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1.  
  2. $(document).ready(function(){
  3.  
  4. $('#chat-message').on('keydown', function(e) {
  5. if (e.which == 13) {
  6. $("#btnSend").click();
  7. }
  8.  
  9. });
  10. });
  11. // 2. This code loads the IFrame Player API code asynchronously.
  12. var tag = document.createElement('script');
  13.  
  14. tag.src = "https://www.youtube.com/iframe_api";
  15. var firstScriptTag = document.getElementsByTagName('script')[0];
  16. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  17.  
  18. // 3. This function creates an <iframe> (and YouTube player)
  19. // after the API code downloads.
  20. var player;
  21. var playerNewState = null;
  22. var firstVideoUrl = null;
  23. var playerFirstCurrenTime = 0;
  24. var setFirstCurrent = false;
  25. var playerIsReady = false;
  26. function onYouTubeIframeAPIReady() {
  27. player = new YT.Player('player', {
  28. height: '360',
  29. width: '640',
  30. events: {
  31. 'onReady': onPlayerReady,
  32. 'onStateChange': onPlayerStateChange,
  33. },
  34. playerVars: {
  35. 'controls': 1,
  36. 'autoplay' : 1
  37. }
  38. });
  39.  
  40. }
  41. function onPlayerReady(event) {
  42. console.log("11");
  43. if(firstVideoUrl != null)
  44. {
  45. let videoId = YouTubeGetID(firstVideoUrl);
  46. $('#player').css('display','block');
  47. player.loadVideoById(videoId);
  48. }
  49.  
  50. playerIsReady = true;
  51.  
  52. }
  53.  
  54.  
  55. function YouTubeGetID(url) {
  56. var ID = '';
  57. url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
  58. if(url[2] !== undefined) {
  59. ID = url[2].split(/[^0-9a-z_\-]/i);
  60. ID = ID[0];
  61. }
  62. else {
  63. ID = url;
  64. }
  65. return ID;
  66. }
  67.  
  68.  
  69.  
  70. var done = false;
  71. function onPlayerStateChange(event) {
  72. if (event.data == YT.PlayerState.PLAYING && !done) {
  73.  
  74. if(playerNewState != null)
  75. {
  76.  
  77.  
  78. if(playerNewState == "pause")
  79. {
  80. player.pauseVideo();
  81.  
  82. }
  83. else
  84. {
  85.  
  86. player.playVideo();
  87.  
  88. }
  89. playerNewState = null;
  90. }
  91. if(setFirstCurrent == false)
  92. {
  93. console.warn("test1" + playerFirstCurrenTime);
  94. player.seekTo(playerFirstCurrenTime);
  95. setFirstCurrent = true;
  96. }
  97.  
  98. done = true;
  99.  
  100. }
  101.  
  102.  
  103.  
  104.  
  105. }
  106. function stopVideo() {
  107. player.stopVideo();
  108. }
  109.  
  110.  
  111. function showMessage(messageHTML) {
  112. $('#chat-box').append(messageHTML);
  113. }
  114. function matchYoutubeUrl(url) {
  115. var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
  116. var matches = url.match(p);
  117. if(matches){
  118. return matches[1];
  119. }
  120. return false;
  121. }
  122. function parseMessage(message){
  123.  
  124.  
  125.  
  126.  
  127.  
  128. var id = matchYoutubeUrl(message);
  129. if(id!=false){
  130. alert(id);
  131. }else{
  132. showMessage("<div class='chat-box-message'>"+message+"</div>");
  133. }
  134.  
  135.  
  136. }
  137.  
  138.  
  139.  
  140. window.addEventListener('load', function(){
  141. var websocket = new WebSocket("ws://10.8.110.243:8090");
  142. websocket.onopen = function(event) {
  143. var messageJSON = {
  144. user_token: userToken,
  145. };
  146. websocket.send(JSON.stringify(messageJSON));
  147. showMessage("<div class='chat-connection-ack'>Connection établie</div>");
  148. }
  149. websocket.onmessage = function(event) {
  150. var Data = JSON.parse(event.data);
  151. //console.debug('data', Data);
  152. if(Data.message_type !== undefined && Data.message !== undefined)
  153. {
  154. console.warn(Data.message);
  155. //parseMessage(Data.message);
  156. }
  157.  
  158. if('firstVideoUrl' in Data)
  159. {
  160.  
  161. firstVideoUrl = Data.firstVideoUrl;
  162. console.warn("22");
  163. if(playerIsReady)
  164. {
  165. let videoId = YouTubeGetID(firstVideoUrl);
  166. $('#player').css('display','block');
  167. player.loadVideoById(videoId);
  168. }
  169. }
  170. if(Data.videoUrl)
  171. {
  172. let videoId = YouTubeGetID(Data.videoUrl);
  173.  
  174.  
  175.  
  176. if(player !== undefined)
  177. {
  178. $('#player').css('display','block');
  179. player.loadVideoById(videoId);
  180. }
  181. }
  182.  
  183. if('firstCurrentTime' in Data) {
  184.  
  185.  
  186. playerFirstCurrenTime = Data.firstCurrentTime;
  187.  
  188.  
  189.  
  190. }
  191.  
  192. if(Data.changePlayerState != undefined)
  193. {
  194. if(Data.changePlayerState == "pause")
  195. {
  196. player.pauseVideo();
  197. }
  198. if(Data.changePlayerState == "play")
  199. {
  200. player.playVideo();
  201.  
  202. }
  203. }
  204.  
  205. if(Data.changePlayerFirstState != undefined)
  206. {
  207.  
  208. if(Data.changePlayerFirstState == "pause")
  209. {
  210. playerNewState = "pause";
  211. }
  212. else
  213. {
  214. playerNewState = "play";
  215.  
  216. }
  217. }
  218.  
  219.  
  220. if(userIsAdmin == true)
  221. {
  222. if(Data.newCurrentTime) {
  223. if (player.getCurrentTime() > 0) {
  224. if(player.getCurrentTime() > Data.newCurrentTime)
  225. {
  226. if((player.getCurrentTime() - Data.newCurrentTime) > 1)
  227. {
  228. player.seekTo(Data.newCurrentTime);
  229. }
  230. }
  231. else if(player.getCurrentTime() < Data.newCurrentTime)
  232. {
  233. if((Data.newCurrentTime - player.getCurrentTime()) > 1)
  234. {
  235. player.seekTo(Data.newCurrentTime);
  236. }
  237. }
  238.  
  239. }
  240.  
  241.  
  242. }
  243. }
  244.  
  245. };
  246.  
  247. websocket.onerror = function(event){
  248. showMessage("<div class='error'>Problem due to some Error</div>");
  249. };
  250. websocket.onclose = function(event){
  251. showMessage("<div class='chat-connection-ack'>Connexion fermée</div>");
  252. };
  253.  
  254. $('#btnSend').click(function(event){
  255. $('#chat-user').attr("type","hidden");
  256. var messageJSON = {
  257. chat_message: $('#chat-message').val()
  258. };
  259. websocket.send(JSON.stringify(messageJSON));
  260. $('#chat-message').val('');
  261. });
  262. $('#btnSendurl').click(function(event){
  263. var messageJSON = {
  264. changeUrl: $('#video-url').val()
  265. };
  266. websocket.send(JSON.stringify(messageJSON));
  267. });
  268. $('#btnPause').click(function(event){
  269. var messageJSON = {
  270. changePlayerState: "pause"
  271. };
  272. websocket.send(JSON.stringify(messageJSON));
  273. });
  274. $('#btnPlay').click(function(event){
  275. var messageJSON = {
  276. changePlayerState: "play"
  277. };
  278. websocket.send(JSON.stringify(messageJSON));
  279. });
  280. if(userIsAdmin == true && playerIsReady)
  281. {
  282. (function(){
  283. var f = function() {
  284. if (player.getCurrentTime() == 0) {
  285. console.log("Aucune vidéo est en cours.");
  286. } else {
  287. //console.log(player.getCurrentTime());
  288. var messageJSON = {
  289. newCurrentTime: player.getCurrentTime()
  290. };
  291. websocket.send(JSON.stringify(messageJSON));
  292. }
  293. };
  294. window.setInterval(f, 1000);
  295. f();
  296. })();
  297. }
  298.  
  299.  
  300. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement