OpenROV /
openrov-cockpit
| 1 | //This file replaces the default app.js to substitute the socket.io server connections |
||
| 2 | //with the webRTC data channel. |
||
| 3 | $(function () { |
||
| 4 | //TODO: Replace this with relative host logic |
||
| 5 | $.getScript('msgpack.min.js'); |
||
| 6 | $.getScript('simplepeer.min.js', function () { |
||
| 7 | var Peer = window.SimplePeer; |
||
| 8 | var io = window.io; |
||
| 9 | var socket = io(window.location.protocol + '//' + window.location.hostname + ':' + window.location.port, { path: '/peerview' }); |
||
| 10 | var peerOpts = { |
||
| 11 | channelConfig: {}, |
||
| 12 | initiator: true, |
||
| 13 | trickle: false |
||
| 14 | }; |
||
| 15 | var heartbeatInterval = null; |
||
| 16 | socket.on('close', function () { |
||
| 17 | if (heartbeatInterval !== null) { |
||
| 18 | clearInterval(heartbeatInterval); |
||
| 19 | } |
||
| 20 | }); |
||
| 21 | socket.on('heartbeat', function (data) { |
||
| 22 | console.log('Heartbeat: ' + JSON.stringify(data)); |
||
| 23 | if (!connected && !connection_pending && data.type == 'server') { |
||
| 24 | connection_pending = true; |
||
| 25 | socket.emit('peer-connect-offer', data.connectionId, function (accepted) { |
||
| 26 | if (accepted) { |
||
| 27 | connected == true; |
||
|
0 ignored issues
–
show
Did you forget to assign or call a function?
This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function: function someFunction(x) {
(x > 0) ? callFoo() : callBar();
}
// JSHint expects you to assign the result to a variable:
function someFunction(x) {
var rs = (x > 0) ? callFoo() : callBar();
}
// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
if (x > 0) {
callFoo();
} else {
callBar();
}
}
Loading history...
|
|||
| 28 | connect(data.connectionId); |
||
| 29 | } |
||
| 30 | }); |
||
| 31 | setTimeout(function () { |
||
| 32 | connection_pending = false; |
||
| 33 | }, 30000); |
||
| 34 | } |
||
| 35 | }); |
||
| 36 | var connected = false; |
||
| 37 | var connection_pending = false; |
||
| 38 | var connect = function (peer_id) { |
||
| 39 | //okay, now we can send the offer |
||
| 40 | var p = new Peer(peerOpts); |
||
| 41 | var emitter = window.cockpit.rov; |
||
| 42 | var self = this; |
||
| 43 | p.withHistory = { |
||
| 44 | on: function (event, fn) { |
||
| 45 | p.on(event, fn); |
||
| 46 | } |
||
| 47 | }; |
||
| 48 | p.on('error', function (err) { |
||
| 49 | console.error(err); |
||
| 50 | socket.off('signal', signalHander); |
||
| 51 | p.destroy(); |
||
| 52 | connection_pending = false; |
||
| 53 | connected = false; |
||
| 54 | }); |
||
| 55 | p.on('signal', function (data) { |
||
| 56 | socket.emit('signal', peer_id, data); |
||
| 57 | console.log('SIGNAL SENT:', JSON.stringify(data)); |
||
| 58 | }); |
||
| 59 | signalHander = function (data, sender_id) { |
||
| 60 | if (sender_id !== peer_id) { |
||
| 61 | console.error('Invalid sender_id'); |
||
| 62 | socket.off('signal', signalHander); |
||
| 63 | p.destroy(); |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | p.signal(data); |
||
| 67 | }; |
||
| 68 | socket.on('signal', signalHander); |
||
| 69 | var chunkByteBuffer = null; |
||
| 70 | const webRTCDataChannelChunkLimit = 16 * 1024; |
||
|
0 ignored issues
–
show
|
|||
| 71 | //16KB Chunk Recommendation |
||
| 72 | processVideoChunk = function (event, chunk_number, remainingBytes, chunk) { |
||
| 73 | chunk = new Uint8Array(chunk); |
||
| 74 | //p.sendemit('x-h264-video.chunk',chunk_count++,data.length-end,chunk); |
||
| 75 | if (chunk_number === 1) { |
||
| 76 | chunkByteBuffer = new Uint8Array(remainingBytes + chunk.length); |
||
| 77 | } |
||
| 78 | chunkByteBuffer.set(chunk, (chunk_number - 1) * webRTCDataChannelChunkLimit); |
||
| 79 | if (remainingBytes === 0) { |
||
| 80 | emitter.emit('x-h264-video.data', chunkByteBuffer.buffer, '!nc!'); |
||
| 81 | } |
||
| 82 | }; |
||
| 83 | p.on('connect', function () { |
||
| 84 | console.log('CONNECT'); |
||
| 85 | $('#t')[0].rovOnline = true; |
||
| 86 | $('#t')[0].userRole = 'View-Only'; |
||
| 87 | connected = true; |
||
| 88 | var handleCloudProfile = function (status) { |
||
| 89 | var userName = 'anonymous'; |
||
| 90 | var photoURL; |
||
| 91 | if (status.loggedIn) { |
||
| 92 | userName = status.profile.name; |
||
| 93 | photoURL = status.profile.picture; |
||
| 94 | } |
||
| 95 | p.send(msgpack.encode([ |
||
| 96 | 'mission-control-register', |
||
| 97 | userName, |
||
| 98 | photoURL |
||
| 99 | ])); |
||
| 100 | }; |
||
| 101 | window.cockpit.withHistory.on('cloudprofile-status', handleCloudProfile); |
||
| 102 | /* //These are forwarded on to the pilot mission control computer for processing |
||
| 103 | [ |
||
| 104 | 'plugin.rovpilot.incrementPowerLevel' |
||
| 105 | ,'plugin.rovpilot.setPowerLevel' |
||
| 106 | ].forEach(function(event){ |
||
| 107 | window.cockpit.on(event,function(){ |
||
| 108 | var args = new Array(arguments.length); |
||
| 109 | for(var i = 0; i < args.length; ++i) { |
||
| 110 | //i is always valid index in the arguments object |
||
| 111 | args[i] = arguments[i]; |
||
| 112 | } |
||
| 113 | p.send(msgpack.encode(['mc-linkedcmd'].concat([event]).concat(args))); |
||
| 114 | }); |
||
| 115 | }); |
||
| 116 | */ |
||
| 117 | p.on('data', function (data) { |
||
| 118 | //where data is an array for emitter events |
||
| 119 | var payload = msgpack.decode(data); |
||
| 120 | switch (payload[0]) { |
||
| 121 | case 'mc-assigned-role': |
||
| 122 | $('#t')[0].userRole = payload[1]; |
||
| 123 | break; |
||
| 124 | case 'mc-ping': |
||
| 125 | p.send(msgpack.encode(['mc-pong',payload[1]])); |
||
| 126 | break; |
||
| 127 | case 'x-h264-video.chunk': |
||
| 128 | processVideoChunk.apply(this, payload); |
||
| 129 | break; |
||
| 130 | case 'CameraRegistration': |
||
| 131 | payload[1].connectionType = 'rov'; |
||
|
0 ignored issues
–
show
|
|||
| 132 | //override the http/socket.io since we will tunnel the traffic through the rov emitter |
||
| 133 | default: |
||
| 134 | emitter.emit.apply(emitter, payload.concat(['!nc!'])); |
||
| 135 | } |
||
| 136 | }); |
||
| 137 | onAnyHandler = function () { |
||
| 138 | if (arguments.length > 0 && arguments[arguments.length - 1] === '!nc!') { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | if (this.event !== 'newListener') { |
||
| 142 | var args = new Array(arguments.length); |
||
| 143 | for (var i = 0; i < args.length; ++i) { |
||
| 144 | //i is always valid index in the arguments object |
||
| 145 | args[i] = arguments[i]; |
||
| 146 | } |
||
| 147 | p.send(msgpack.encode([this.event].concat(args))); |
||
| 148 | } |
||
| 149 | }; |
||
| 150 | emitter.onAny(onAnyHandler); |
||
| 151 | //For testing binary transfer limits |
||
| 152 | ondataMsgHandler = function (size, data) { |
||
| 153 | if (data === undefined || data.byteLength === undefined) { |
||
| 154 | console.error('DATA-MSG: Test packet ' + size + ' failed'); |
||
| 155 | return; |
||
| 156 | } |
||
| 157 | if (data.byteLength === size) { |
||
| 158 | console.log('DATA-MSG: Test packet ' + size + ' worked'); |
||
| 159 | } else { |
||
| 160 | console.error('DATA-MSG: Test packet ' + size + ' wrong size'); |
||
| 161 | } |
||
| 162 | }; |
||
| 163 | emitter.on('data-msg', ondataMsgHandler); |
||
| 164 | p.on('close', function () { |
||
| 165 | socket.off('signal', signalHander); |
||
| 166 | emitter.offAny(onAnyHandler); |
||
| 167 | window.cockpit.off('cloudprofile-status', handleCloudProfile); |
||
| 168 | emitter.off('data-msg', ondataMsgHandler); |
||
| 169 | connected = false; |
||
| 170 | console.log('Connection to peer closed'); |
||
| 171 | //TODO: Architect the system to better handle new ROV connections |
||
| 172 | if (connected) { |
||
| 173 | location.reload(); |
||
| 174 | } |
||
| 175 | }); |
||
| 176 | cockpit.peerConnected = true; |
||
| 177 | window.cockpit.rov.connection = 'p2p'; |
||
| 178 | cockpit.rov.on('cockpit.pluginsLoaded', function () { |
||
| 179 | }); |
||
| 180 | }); |
||
| 181 | return p; |
||
| 182 | }; |
||
| 183 | socket.on('connect', function () { |
||
| 184 | heartbeatInterval = setInterval(function () { |
||
| 185 | socket.emit('heartbeat', 'viewer'); |
||
| 186 | }, 10000); |
||
| 187 | }); |
||
| 188 | }); |
||
| 189 | $('#t')[0].__ = function (str) { |
||
| 190 | return str; |
||
| 191 | }; |
||
| 192 | }); |
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.
Read more about comparison operations.