obs-websocket-js
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -1,3 +0,4 @@ | ||
/*! | ||
* OBS WebSocket Javascript API (obs-websocket-js) v0.1.0 | ||
(function(){ | ||
/* | ||
* OBS WebSocket Javascript API (obs-websocket-js) v0.1.1 | ||
* Author: Brendan Hagan (haganbmj) | ||
@@ -51,3 +52,3 @@ * Repo: git+https://github.com/haganbmj/obs-websocket-js.git | ||
this.name = (typeof name === 'undefined') ? '' : name; | ||
this.sources = (typeof sources === 'undefined') ? [] : sources; | ||
sources = (typeof sources === 'undefined') ? [] : sources; | ||
@@ -79,2 +80,13 @@ var self = this; | ||
var OBSSource = {}; | ||
var OBSScene = {}; | ||
if (isModule()) { | ||
OBSSource = module.exports.OBSSource; | ||
OBSScene = module.exports.OBSScene; | ||
} else { | ||
OBSSource = window.OBSSource; | ||
OBSScene = window.OBSScene; | ||
} | ||
/** | ||
@@ -86,246 +98,231 @@ * @class OBSWebSocket | ||
*/ | ||
(function() { | ||
var OBSSource = {}; | ||
var OBSScene = {}; | ||
function OBSWebSocket() { | ||
OBSWebSocket.DEFAULT_PORT = 4444; | ||
OBSWebSocket.CONSOLE_NAME = '[OBSWebSocket]'; | ||
if (isModule()) { | ||
OBSSource = module.exports.OBSSource; | ||
OBSScene = module.exports.OBSScene; | ||
} else { | ||
OBSSource = window.OBSSource; | ||
OBSScene = window.OBSScene; | ||
} | ||
this._connected = false; | ||
this._socket = undefined; | ||
this._requestCounter = 0; | ||
this._responseCallbacks = {}; | ||
this._auth = { salt: '', challenge: '' }; | ||
} | ||
function OBSWebSocket() { | ||
OBSWebSocket.DEFAULT_PORT = 4444; | ||
OBSWebSocket.CONSOLE_NAME = '[OBSWebSocket]'; | ||
var WebSocket = {}; | ||
this._connected = false; | ||
this._socket = undefined; | ||
this._requestCounter = 0; | ||
this._responseCallbacks = {}; | ||
this._auth = { salt: '', challenge: '' }; | ||
} | ||
if (isModule()) { | ||
WebSocket = require('ws'); | ||
} else { | ||
WebSocket = window.WebSocket; | ||
} | ||
var WebSocket = {}; | ||
OBSWebSocket.prototype._generateMessageId = function() { | ||
this._requestCounter++; | ||
return this._requestCounter + ''; | ||
}; | ||
if (isModule()) { | ||
WebSocket = require('ws'); | ||
OBSWebSocket.prototype._sendRequest = function(requestType, args, callback) { | ||
if (this._connected) { | ||
args = args || {}; | ||
callback = callback || function() {}; | ||
args['message-id'] = this._generateMessageId(); | ||
args['request-type'] = requestType; | ||
this._responseCallbacks[args['message-id']] = { | ||
'requestType': requestType, | ||
'callbackFunction': callback | ||
}; | ||
this._socket.send(JSON.stringify(args)); | ||
} else { | ||
WebSocket = window.WebSocket; | ||
console.warn(OBSWebSocket.CONSOLE_NAME, "Not connected."); | ||
} | ||
}; | ||
OBSWebSocket.prototype._generateMessageId = function() { | ||
this._requestCounter++; | ||
return this._requestCounter + ''; | ||
}; | ||
OBSWebSocket.prototype._onMessage = function(msg) { | ||
var message = JSON.parse(msg.data); | ||
var err = null; | ||
OBSWebSocket.prototype._sendRequest = function(requestType, args, callback) { | ||
if (this._connected) { | ||
args = args || {}; | ||
callback = callback || function() {}; | ||
args['message-id'] = this._generateMessageId(); | ||
args['request-type'] = requestType; | ||
if (!message) | ||
return; | ||
this._responseCallbacks[args['message-id']] = { | ||
'requestType': requestType, | ||
'callbackFunction': callback | ||
}; | ||
var updateType = message['update-type']; | ||
var messageId = message['message-id']; | ||
this._socket.send(JSON.stringify(args)); | ||
} else { | ||
console.warn(OBSWebSocket.CONSOLE_NAME, "Not connected."); | ||
} | ||
}; | ||
if (message.status === 'error') { | ||
console.error(OBSWebSocket.CONSOLE_NAME, 'Error:', message.error); | ||
err = message.error; | ||
message = null; | ||
} | ||
OBSWebSocket.prototype._onMessage = function(msg) { | ||
var message = JSON.parse(msg.data); | ||
var err = null; | ||
if (!message) | ||
return; | ||
var updateType = message['update-type']; | ||
var messageId = message['message-id']; | ||
if (message.status === 'error') { | ||
console.error(OBSWebSocket.CONSOLE_NAME, 'Error:', message.error); | ||
err = message.error; | ||
message = null; | ||
if (updateType) { | ||
if (message) { | ||
this._buildEventCallback(updateType, message); | ||
} | ||
} else { | ||
var callback = this._responseCallbacks[messageId].callbackFunction; | ||
if (updateType) { | ||
if (message) { | ||
this._buildEventCallback(updateType, message); | ||
} | ||
} else { | ||
var callback = this._responseCallbacks[messageId].callbackFunction; | ||
if (callback) { | ||
callback(err, message); | ||
} | ||
delete this._responseCallbacks[messageId]; | ||
if (callback) { | ||
callback(err, message); | ||
} | ||
}; | ||
OBSWebSocket.prototype._buildEventCallback = function(updateType, message) { | ||
var self = this; | ||
delete this._responseCallbacks[messageId]; | ||
} | ||
}; | ||
switch(updateType) { | ||
case 'SwitchScenes': | ||
this.onSceneSwitch(message['scene-name']); | ||
return; | ||
case 'ScenesChanged': | ||
this.getSceneList(function(sceneList) { | ||
self.onSceneListChanged(sceneList); | ||
}); | ||
return; | ||
case 'StreamStarting': | ||
this.onStreamStarting(); | ||
return; | ||
case 'StreamStarted': | ||
this.onStreamStarted(); | ||
return; | ||
case 'StreamStopping': | ||
this.onStreamStopping(); | ||
return; | ||
case 'StreamStopped': | ||
this.onStreamStopped(); | ||
return; | ||
case 'RecordingStarting': | ||
this.onRecordingStarting(); | ||
return; | ||
case 'RecordingStarted': | ||
this.onRecordingStarted(); | ||
return; | ||
case 'RecordingStopping': | ||
this.onRecordingStopping(); | ||
return; | ||
case 'RecordingStopped': | ||
this.onRecordingStopped(); | ||
return; | ||
case 'StreamStatus': | ||
message['bytesPerSecond'] = message['bytes-per-sec']; | ||
message['totalStreamTime'] = message['total-stream-time']; | ||
message['numberOfFrames'] = message['num-total-frames']; | ||
message['numberOfDroppedFrames'] = message['num-dropped-frames']; | ||
this.onStreamStatus(message); | ||
return; | ||
case 'Exiting': | ||
this.onExit(); | ||
return; | ||
default: | ||
console.warn(OBSWebSocket.CONSOLE_NAME, 'Unknown UpdateType:', updateType, message); | ||
} | ||
}; | ||
OBSWebSocket.prototype._buildEventCallback = function(updateType, message) { | ||
var self = this; | ||
if (isModule()) { | ||
module.exports.OBSWebSocket = OBSWebSocket; | ||
} else { | ||
window.OBSWebSocket = OBSWebSocket; | ||
switch(updateType) { | ||
case 'SwitchScenes': | ||
this.onSceneSwitch(message['scene-name']); | ||
return; | ||
case 'ScenesChanged': | ||
this.getSceneList(function(sceneList) { | ||
self.onSceneListChanged(sceneList); | ||
}); | ||
return; | ||
case 'StreamStarting': | ||
this.onStreamStarting(); | ||
return; | ||
case 'StreamStarted': | ||
this.onStreamStarted(); | ||
return; | ||
case 'StreamStopping': | ||
this.onStreamStopping(); | ||
return; | ||
case 'StreamStopped': | ||
this.onStreamStopped(); | ||
return; | ||
case 'RecordingStarting': | ||
this.onRecordingStarting(); | ||
return; | ||
case 'RecordingStarted': | ||
this.onRecordingStarted(); | ||
return; | ||
case 'RecordingStopping': | ||
this.onRecordingStopping(); | ||
return; | ||
case 'RecordingStopped': | ||
this.onRecordingStopped(); | ||
return; | ||
case 'StreamStatus': | ||
message['bytesPerSecond'] = message['bytes-per-sec']; | ||
message['totalStreamTime'] = message['total-stream-time']; | ||
message['numberOfFrames'] = message['num-total-frames']; | ||
message['numberOfDroppedFrames'] = message['num-dropped-frames']; | ||
this.onStreamStatus(message); | ||
return; | ||
case 'Exiting': | ||
this.onExit(); | ||
return; | ||
default: | ||
console.warn(OBSWebSocket.CONSOLE_NAME, 'Unknown UpdateType:', updateType, message); | ||
} | ||
})(); | ||
}; | ||
(function() { | ||
OBSWebSocket.prototype._webCryptoHash = function(pass, callback) { | ||
var self = this; | ||
if (isModule()) { | ||
module.exports.OBSWebSocket = OBSWebSocket; | ||
} else { | ||
window.OBSWebSocket = OBSWebSocket; | ||
} | ||
var utf8Pass = _encodeStringAsUTF8(pass); | ||
var utf8Salt = _encodeStringAsUTF8(this._auth.salt); | ||
var ab1 = _stringToArrayBuffer(utf8Pass + utf8Salt); | ||
OBSWebSocket.prototype._webCryptoHash = function(pass, callback) { | ||
var self = this; | ||
crypto.subtle.digest('SHA-256', ab1) | ||
.then(function(authHash) { | ||
var utf8AuthHash = _encodeStringAsUTF8(_arrayBufferToBase64(authHash)); | ||
var utf8Challenge = _encodeStringAsUTF8(self._auth.challenge); | ||
var ab2 = _stringToArrayBuffer(utf8AuthHash + utf8Challenge); | ||
var utf8Pass = _encodeStringAsUTF8(pass); | ||
var utf8Salt = _encodeStringAsUTF8(this._auth.salt); | ||
var ab1 = _stringToArrayBuffer(utf8Pass + utf8Salt); | ||
crypto.subtle.digest('SHA-256', ab2) | ||
.then(function(authResp) { | ||
var authRespB64 = _arrayBufferToBase64(authResp); | ||
callback(authRespB64); | ||
}); | ||
}); | ||
}; | ||
crypto.subtle.digest('SHA-256', ab1) | ||
.then(function(authHash) { | ||
var utf8AuthHash = _encodeStringAsUTF8(_arrayBufferToBase64(authHash)); | ||
var utf8Challenge = _encodeStringAsUTF8(self._auth.challenge); | ||
var ab2 = _stringToArrayBuffer(utf8AuthHash + utf8Challenge); | ||
OBSWebSocket.prototype._cryptoJSHash = function(pass, callback) { | ||
var utf8Pass = _encodeStringAsUTF8(pass); | ||
var utf8Salt = _encodeStringAsUTF8(this._auth.salt); | ||
crypto.subtle.digest('SHA-256', ab2) | ||
.then(function(authResp) { | ||
var authRespB64 = _arrayBufferToBase64(authResp); | ||
callback(authRespB64); | ||
}); | ||
}); | ||
}; | ||
var authHash = CryptoJS.SHA256(utf8Pass + utf8Salt).toString(CryptoJS.enc.Base64); | ||
OBSWebSocket.prototype._cryptoJSHash = function(pass, callback) { | ||
var utf8Pass = _encodeStringAsUTF8(pass); | ||
var utf8Salt = _encodeStringAsUTF8(this._auth.salt); | ||
var utf8AuthHash = _encodeStringAsUTF8(authHash); | ||
var utf8Challenge = _encodeStringAsUTF8(this._auth.challenge); | ||
var authHash = CryptoJS.SHA256(utf8Pass + utf8Salt).toString(CryptoJS.enc.Base64); | ||
var authResp = CryptoJS.SHA256(utf8AuthHash + utf8Challenge).toString(CryptoJS.enc.Base64); | ||
callback(authResp); | ||
}; | ||
var utf8AuthHash = _encodeStringAsUTF8(authHash); | ||
var utf8Challenge = _encodeStringAsUTF8(this._auth.challenge); | ||
OBSWebSocket.prototype._nodeCryptoHash = function(pass, callback) { | ||
var authHasher = crypto.createHash('sha256'); | ||
var authResp = CryptoJS.SHA256(utf8AuthHash + utf8Challenge).toString(CryptoJS.enc.Base64); | ||
callback(authResp); | ||
}; | ||
var utf8Pass = _encodeStringAsUTF8(pass); | ||
var utf8Salt = _encodeStringAsUTF8(this._auth.salt); | ||
OBSWebSocket.prototype._nodeCryptoHash = function(pass, callback) { | ||
var authHasher = crypto.createHash('sha256'); | ||
authHasher.update(utf8Pass + utf8Salt); | ||
var authHash = authHasher.digest('base64'); | ||
var utf8Pass = _encodeStringAsUTF8(pass); | ||
var utf8Salt = _encodeStringAsUTF8(this._auth.salt); | ||
var respHasher = crypto.createHash('sha256'); | ||
authHasher.update(utf8Pass + utf8Salt); | ||
var authHash = authHasher.digest('base64'); | ||
var utf8AuthHash = _encodeStringAsUTF8(authHash); | ||
var utf8Challenge = _encodeStringAsUTF8(this._auth.challenge); | ||
var respHasher = crypto.createHash('sha256'); | ||
respHasher.update(utf8AuthHash + utf8Challenge); | ||
var respHash = respHasher.digest('base64'); | ||
var utf8AuthHash = _encodeStringAsUTF8(authHash); | ||
var utf8Challenge = _encodeStringAsUTF8(this._auth.challenge); | ||
callback(respHash); | ||
}; | ||
respHasher.update(utf8AuthHash + utf8Challenge); | ||
var respHash = respHasher.digest('base64'); | ||
function _encodeStringAsUTF8(string) { | ||
return unescape(encodeURIComponent(string)); | ||
} | ||
callback(respHash); | ||
}; | ||
function _stringToArrayBuffer(string) { | ||
var ret = new Uint8Array(string.length); | ||
for (var i = 0; i < string.length; i++) { | ||
ret[i] = string.charCodeAt(i); | ||
} | ||
function _encodeStringAsUTF8(string) { | ||
return unescape(encodeURIComponent(string)); | ||
} | ||
return ret.buffer; | ||
function _stringToArrayBuffer(string) { | ||
var ret = new Uint8Array(string.length); | ||
for (var i = 0; i < string.length; i++) { | ||
ret[i] = string.charCodeAt(i); | ||
} | ||
function _arrayBufferToBase64(arrayBuffer) { | ||
var binary = ''; | ||
var bytes = new Uint8Array(arrayBuffer); | ||
return ret.buffer; | ||
} | ||
var length = bytes.byteLength; | ||
for (var i = 0; i < length; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
function _arrayBufferToBase64(arrayBuffer) { | ||
var binary = ''; | ||
var bytes = new Uint8Array(arrayBuffer); | ||
return btoa(binary); | ||
var length = bytes.byteLength; | ||
for (var i = 0; i < length; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
var crypto = {}; | ||
return btoa(binary); | ||
} | ||
if (isModule()) { | ||
crypto = require('crypto'); | ||
OBSWebSocket.prototype._authHash = OBSWebSocket.prototype._nodeCryptoHash; | ||
} else { | ||
crypto = window.crypto || window.msCrypto || {}; | ||
OBSWebSocket.prototype._authHash = OBSWebSocket.prototype._webCryptoHash; | ||
var crypto = {}; | ||
if (typeof crypto.subtle === 'undefined') { | ||
if (typeof crypto.webkitSubtle === 'undefined') { | ||
if (typeof CryptoJS === 'undefined') { | ||
throw new Error('OBS WebSocket requires CryptoJS when native crypto is unavailable.'); | ||
} | ||
OBSWebSocket.prototype._authHash = OBSWebSocket.prototype._cryptoJSHash; | ||
} else { | ||
crypto.subtle = crypto.webkitSubtle; | ||
if (isModule()) { | ||
crypto = require('crypto'); | ||
OBSWebSocket.prototype._authHash = OBSWebSocket.prototype._nodeCryptoHash; | ||
} else { | ||
crypto = window.crypto || window.msCrypto || {}; | ||
OBSWebSocket.prototype._authHash = OBSWebSocket.prototype._webCryptoHash; | ||
if (typeof crypto.subtle === 'undefined') { | ||
if (typeof crypto.webkitSubtle === 'undefined') { | ||
if (typeof CryptoJS === 'undefined') { | ||
throw new Error('OBS WebSocket requires CryptoJS when native crypto is unavailable.'); | ||
} | ||
OBSWebSocket.prototype._authHash = OBSWebSocket.prototype._cryptoJSHash; | ||
} else { | ||
crypto.subtle = crypto.webkitSubtle; | ||
} | ||
} | ||
})(); | ||
} | ||
@@ -572,3 +569,3 @@ /** | ||
this._socket = new WebSocket('ws://' + address, ['soap', 'xmpp']); | ||
this._socket = new WebSocket('ws://' + address); | ||
@@ -839,1 +836,2 @@ this._socket.onopen = function() { | ||
}; | ||
})(); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('./dist/obs-websocket-js'); | ||
module.exports = require('./dist/obs-websocket.js'); |
{ | ||
"name": "obs-websocket-js", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "OBS Websocket API in Javascript", | ||
@@ -34,2 +34,3 @@ "main": "index.js", | ||
"grunt": "^1.0.1", | ||
"grunt-anonymous": "^0.1.2", | ||
"grunt-contrib-clean": "^1.0.0", | ||
@@ -36,0 +37,0 @@ "grunt-contrib-concat": "^1.0.1", |
41934
8
723