1 | /* jshint node: true */
|
2 | ;
|
3 |
|
4 | /**
|
5 | # freeice
|
6 |
|
7 | The `freeice` module is a simple way of getting random STUN or TURN server
|
8 | for your WebRTC application. The list of servers (just STUN at this stage)
|
9 | were sourced from this [gist](https://gist.github.com/zziuni/3741933).
|
10 |
|
11 | ## Example Use
|
12 |
|
13 | The following demonstrates how you can use `freeice` with
|
14 | [rtc-quickconnect](https://github.com/rtc-io/rtc-quickconnect):
|
15 |
|
16 | <<< examples/quickconnect.js
|
17 |
|
18 | As the `freeice` module generates ice servers in a list compliant with the
|
19 | WebRTC spec you will be able to use it with raw `RTCPeerConnection`
|
20 | constructors and other WebRTC libraries.
|
21 |
|
22 | ## Hey, don't use my STUN/TURN server!
|
23 |
|
24 | If for some reason your free STUN or TURN server ends up in the
|
25 | [list](servers.js) of servers that is used in this module, you can feel
|
26 | free to open an issue on this repository and those servers will be removed
|
27 | within 24 hours (or sooner). This is the quickest and probably the most
|
28 | polite way to have something removed (and provides us some visibility
|
29 | if someone opens a pull request requesting that a server is added).
|
30 |
|
31 | ## Please add my server!
|
32 |
|
33 | If you have a server that you wish to add to the list, that's awesome! I'm
|
34 | sure I speak on behalf of a whole pile of WebRTC developers who say thanks.
|
35 | To get it into the list, feel free to either open a pull request or if you
|
36 | find that process a bit daunting then just create an issue requesting
|
37 | the addition of the server (make sure you provide all the details, and if
|
38 | you have a Terms of Service then including that in the PR/issue would be
|
39 | awesome).
|
40 |
|
41 | ## I know of a free server, can I add it?
|
42 |
|
43 | Sure, if you do your homework and make sure it is ok to use (I'm currently
|
44 | in the process of reviewing the terms of those STUN servers included from
|
45 | the original list). If it's ok to go, then please see the previous entry
|
46 | for how to add it.
|
47 |
|
48 | ## Current List of Servers
|
49 |
|
50 | * current as at the time of last `README.md` file generation
|
51 |
|
52 | <<< servers.js
|
53 |
|
54 | **/
|
55 |
|
56 | var freeice = module.exports = function(opts) {
|
57 | // if a list of servers has been provided, then use it instead of defaults
|
58 | var servers = (opts || {}).servers || require('./servers');
|
59 |
|
60 | var stunCount = (opts || {}).stun || 1;
|
61 | var turnCount = (opts || {}).turn || 0;
|
62 | var selected;
|
63 |
|
64 | function getServers(type, count) {
|
65 | var out = [];
|
66 | var input = [].concat(servers[type]);
|
67 | var idx;
|
68 |
|
69 | while (input.length && out.length < count) {
|
70 | idx = (Math.random() * input.length) | 0;
|
71 | out = out.concat(input.splice(idx, 1));
|
72 | }
|
73 |
|
74 | return out.map(function(url) {
|
75 | return { url: type + ':' + url };
|
76 | });
|
77 | }
|
78 |
|
79 | // add stun servers
|
80 | selected = [].concat(getServers('stun', stunCount));
|
81 |
|
82 | if (turnCount) {
|
83 | selected = selected.concat(getServers('turn', turnCount));
|
84 | }
|
85 |
|
86 | return selected;
|
87 | }; |
\ | No newline at end of file |