1468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org//
3468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// Use of this source code is governed by a BSD-style license
4468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// that can be found in the LICENSE file in the root of the source
5468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// tree. An additional intellectual property rights grant can be found
6468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// in the file PATENTS.  All contributing project authors may
7468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// be found in the AUTHORS file in the root of the source tree.
8468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org//
9468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// This file exposes the api for the bot to connect to the host script
10468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// waiting a websocket connection and using dnode for javascript rpc.
11468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org//
12468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// This file is served to the browser via browserify to resolve the
13468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// dnode requires.
14468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.orgvar WebSocketStream = require('websocket-stream');
15468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.orgvar Dnode = require('dnode');
16468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org
17468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.orgfunction connectToServer(api) {
18e9b7d03db6dad08f1c160878df2cbc9b680ba7dbhoussainy@google.com  var stream = new WebSocketStream("wss://localhost:8080/");
19468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org  var dnode = new Dnode(api);
20468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org  dnode.on('error', function (error) { console.log(error); });
21468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org  dnode.pipe(stream).pipe(dnode);
22468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org}
23468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org
24468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// Dnode loses certain method calls when exposing native browser objects such as
25468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// peer connections. This methods helps work around that by allowing one to
26468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// redefine a non-native method in a target "obj" from "src" that applies a list
27468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org// of casts to the arguments (types are lost in dnode).
28468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.orgfunction expose(obj, src, method, casts) {
29468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org  obj[method] = function () {
30468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org    for (index in casts)
31468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org      arguments[index] = new (casts[index])(arguments[index]);
32468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org    src[method].apply(src, arguments);
33468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org  }
34468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org}
35468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.org
36468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.orgwindow.expose = expose;
37468516c9594bc8490de40be7ffac7069fa24a5a3andresp@webrtc.orgwindow.connectToServer = connectToServer;
38