13dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
23dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org//
33dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// Use of this source code is governed by a BSD-style license
43dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// that can be found in the LICENSE file in the root of the source
53dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// tree. An additional intellectual property rights grant can be found
63dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// in the file PATENTS.  All contributing project authors may
73dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// be found in the AUTHORS file in the root of the source tree.
83dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org//
93dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// This file exposes the api for the bot to connect to the host script
103dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// waiting a websocket connection and using dnode for javascript rpc.
113dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org//
123dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// This file is served to the browser via browserify to resolve the
133dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// dnode requires.
143dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgvar WebSocketStream = require('websocket-stream');
153dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgvar Dnode = require('dnode');
163dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org
173dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgfunction connectToServer(api) {
183dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org  var stream = new WebSocketStream("ws://127.0.0.1:8080/");
193dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org  var dnode = new Dnode(api);
203dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org  dnode.on('error', function (error) { console.log(error); });
213dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org  dnode.pipe(stream).pipe(dnode);
223dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org}
233dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org
243dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// Dnode loses certain method calls when exposing native browser objects such as
253dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// peer connections. This methods helps work around that by allowing one to
263dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// redefine a non-native method in a target "obj" from "src" that applies a list
273dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org// of casts to the arguments (types are lost in dnode).
283dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgfunction expose(obj, src, method, casts) {
293dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org  obj[method] = function () {
303dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org    for (index in casts)
313dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org      arguments[index] = new (casts[index])(arguments[index]);
323dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org    src[method].apply(src, arguments);
333dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org  }
343dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org}
353dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.org
363dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgwindow.expose = expose;
373dbd81358e71cc749fb730b27ccfb73a79aeff88andresp@webrtc.orgwindow.connectToServer = connectToServer;
38