srs.sig.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2013-2021 Winlin
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. 'use strict';
  24. // Async-await-promise based SRS RTC Signaling.
  25. function SrsRtcSignalingAsync() {
  26. var self = {};
  27. // The schema is ws or wss, host is ip or ip:port, display is nickname
  28. // of user to join the room.
  29. self.connect = async function (schema, host, room, display) {
  30. var url = schema + '://' + host + '/sig/v1/rtc';
  31. self.ws = new WebSocket(url + '?room=' + room + '&display=' + display);
  32. self.ws.onmessage = function(event) {
  33. var r = JSON.parse(event.data);
  34. var promise = self._internals.msgs[r.tid];
  35. if (promise) {
  36. promise.resolve(r.msg);
  37. delete self._internals.msgs[r.tid];
  38. } else {
  39. self.onmessage(r.msg);
  40. }
  41. };
  42. return new Promise(function (resolve, reject) {
  43. self.ws.onopen = function (event) {
  44. resolve(event);
  45. };
  46. self.ws.onerror = function (event) {
  47. reject(event);
  48. };
  49. });
  50. };
  51. // The message is a json object.
  52. self.send = async function (message) {
  53. return new Promise(function (resolve, reject) {
  54. var r = {tid: Number(parseInt(new Date().getTime()*Math.random()*100)).toString(16).slice(0, 7), msg: message};
  55. self._internals.msgs[r.tid] = {resolve: resolve, reject: reject};
  56. self.ws.send(JSON.stringify(r));
  57. });
  58. };
  59. self.close = function () {
  60. self.ws && self.ws.close();
  61. self.ws = null;
  62. for (const tid in self._internals.msgs) {
  63. var promise = self._internals.msgs[tid];
  64. promise.reject('close');
  65. }
  66. };
  67. // The callback when got messages from signaling server.
  68. self.onmessage = function (msg) {
  69. };
  70. self._internals = {
  71. // Key is tid, value is object {resolve, reject, response}.
  72. msgs: {}
  73. };
  74. return self;
  75. }
  76. // Parse params in query string.
  77. function SrsRtcSignalingParse(location) {
  78. let query = location.href.split('?')[1];
  79. query = query? '?' + query : null;
  80. let wsSchema = location.href.split('wss=')[1];
  81. wsSchema = wsSchema? wsSchema.split('&')[0] : (location.protocol === 'http:'? 'ws' : 'wss');
  82. let wsHost = location.href.split('wsh=')[1];
  83. wsHost = wsHost? wsHost.split('&')[0] : location.hostname;
  84. let wsPort = location.href.split('wsp=')[1];
  85. wsPort = wsPort? wsPort.split('&')[0] : location.host.split(':')[1];
  86. let host = location.href.split('host=')[1];
  87. host = host? host.split('&')[0] : location.hostname;
  88. let room = location.href.split('room=')[1];
  89. room = room? room.split('&')[0] : null;
  90. let display = location.href.split('display=')[1];
  91. display = display? display.split('&')[0] : Number(parseInt(new Date().getTime()*Math.random()*100)).toString(16).toString(16).slice(0, 7);
  92. let autostart = location.href.split('autostart=')[1];
  93. autostart = autostart && autostart.split('&')[0] === 'true';
  94. // Remove data in query.
  95. let rawQuery = query;
  96. if (query) {
  97. query = query.replace('wss=' + wsSchema, '');
  98. query = query.replace('wsh=' + wsHost, '');
  99. query = query.replace('wsp=' + wsPort, '');
  100. query = query.replace('host=' + host, '');
  101. if (room) {
  102. query = query.replace('room=' + room, '');
  103. }
  104. query = query.replace('display=' + display, '');
  105. query = query.replace('autostart=' + autostart, '');
  106. while (query.indexOf('&&') >= 0) {
  107. query = query.replace('&&', '&');
  108. }
  109. query = query.replace('?&', '?');
  110. if (query.lastIndexOf('?') === query.length - 1) {
  111. query = query.slice(0, query.length - 1);
  112. }
  113. if (query.lastIndexOf('&') === query.length - 1) {
  114. query = query.slice(0, query.length - 1);
  115. }
  116. }
  117. // Regenerate the host of websocket.
  118. wsHost = wsPort? wsHost.split(':')[0] + ':' + wsPort : wsHost;
  119. return {
  120. query: query, rawQuery: rawQuery, wsSchema: wsSchema, wsHost: wsHost, host: host,
  121. room: room, display: display, autostart: autostart,
  122. };
  123. }