andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 1 | // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | // |
| 9 | // This file exposes the api for the bot to connect to the host script |
| 10 | // waiting a websocket connection and using dnode for javascript rpc. |
| 11 | // |
| 12 | // This file is served to the browser via browserify to resolve the |
| 13 | // dnode requires. |
| 14 | var WebSocketStream = require('websocket-stream'); |
| 15 | var Dnode = require('dnode'); |
| 16 | |
| 17 | function connectToServer(api) { |
| 18 | var stream = new WebSocketStream("ws://127.0.0.1:8080/"); |
| 19 | var dnode = new Dnode(api); |
| 20 | dnode.on('error', function (error) { console.log(error); }); |
| 21 | dnode.pipe(stream).pipe(dnode); |
| 22 | } |
| 23 | |
| 24 | // Dnode loses certain method calls when exposing native browser objects such as |
| 25 | // peer connections. This methods helps work around that by allowing one to |
| 26 | // redefine a non-native method in a target "obj" from "src" that applies a list |
| 27 | // of casts to the arguments (types are lost in dnode). |
| 28 | function expose(obj, src, method, casts) { |
| 29 | obj[method] = function () { |
| 30 | for (index in casts) |
| 31 | arguments[index] = new (casts[index])(arguments[index]); |
| 32 | src[method].apply(src, arguments); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | window.expose = expose; |
| 37 | window.connectToServer = connectToServer; |