Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

chance of api fic

  • Loading branch information...
commit 04fd8124438cd6f3a421f224cb5db4f39a359345 1 parent a404d7a
authored August 11, 2012
2  example/index.html
@@ -131,9 +131,11 @@
131 131
           if (key === 13) {
132 132
             rtc._socket.send(JSON.stringify({
133 133
               "eventName": "chat_msg",
  134
+              "data": {
134 135
               "messages": input.value,
135 136
               "room": room,
136 137
               "color": color
  138
+              }
137 139
             }), function(error) {
138 140
               if (error) {
139 141
                 console.log(error);
8  example/server.js
@@ -36,13 +36,15 @@ webRTC.rtc.on('chat_msg', function(data, socket) {
36 36
     var socketId = roomList[i];
37 37
 
38 38
     if (socketId !== socket.id) {
39  
-      var soc = webRTC.rtc.getSocket(data.room, socketId);
  39
+      var soc = webRTC.rtc.getSocket(socketId);
40 40
 
41 41
       if (soc) {
42 42
         soc.send(JSON.stringify({
43 43
           "eventName": "receive_chat_msg",
44  
-          "messages": data.messages,
45  
-          "color": data.color
  44
+          "data": {
  45
+            "messages": data.messages,
  46
+            "color": data.color
  47
+          }
46 48
         }), function(error) {
47 49
           if (error) {
48 50
             console.log(error);
297  example/webrtc.io.js
... ...
@@ -1,297 +0,0 @@
1  
-//CLIENT
2  
-
3  
- // Fallbacks for vendor-specific variables until the spec is finalized.
4  
-var PeerConnection = window.PeerConnection || window.webkitPeerConnection00;
5  
-var URL = window.URL || window.webkitURL || window.msURL || window.oURL;
6  
-var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
7  
-
8  
-(function() {
9  
-
10  
-  var rtc;
11  
-  if ('undefined' === typeof module) {
12  
-    rtc = this.rtc = {};
13  
-  } else {
14  
-    rtc = module.exports = {};
15  
-  }
16  
-
17  
- 
18  
-  // Holds a connection to the server.
19  
-  rtc._socket = null;
20  
-
21  
-  // Holds callbacks for certain events.
22  
-  rtc._events = {};
23  
-
24  
-  rtc.on = function(eventName, callback) {
25  
-    rtc._events[eventName] = rtc._events[eventName] || [];
26  
-    rtc._events[eventName].push(callback);
27  
-  };
28  
-
29  
-  rtc.fire = function(eventName, _) {
30  
-    var events = rtc._events[eventName];
31  
-    var args = Array.prototype.slice.call(arguments, 1);
32  
-
33  
-    if (!events) {
34  
-      return;
35  
-    }
36  
-
37  
-    for (var i = 0, len = events.length; i < len; i++) {
38  
-      events[i].apply(null, args);
39  
-    }
40  
-  };
41  
-
42  
-  // Holds the STUN server to use for PeerConnections.
43  
-  rtc.SERVER = "STUN stun.l.google.com:19302";
44  
-
45  
-  // Referenc e to the lone PeerConnection instance.
46  
-  rtc.peerConnections = {};
47  
-
48  
-  // Array of known peer socket ids
49  
-  rtc.connections = [];
50  
-  // Stream-related variables.
51  
-  rtc.streams = [];
52  
-  rtc.numStreams = 0;
53  
-  rtc.initializedStreams = 0;
54  
-
55  
-  /**
56  
-   * Connects to the websocket server.
57  
-   */
58  
-  rtc.connect = function(server, room) {
59  
-    room = room || ""; // by default, join a room called the blank string
60  
-    rtc._socket = new WebSocket(server);
61  
-
62  
-    rtc._socket.onopen = function() {
63  
-
64  
-      rtc._socket.send(JSON.stringify({
65  
-        "eventName": "join_room",
66  
-        "room": room
67  
-      }), function(error){
68  
-          if(error){console.log(error);}
69  
-        });
70  
-
71  
-      rtc._socket.onmessage = function(data) {
72  
-        var json = JSON.parse(data.data);
73  
-        rtc.fire(json.eventName, json);
74  
-      };
75  
-
76  
-      rtc._socket.onerror = function(err) {
77  
-        console.log('onerror');
78  
-        console.log(err);
79  
-      };
80  
-
81  
-      rtc._socket.onclose = function(data) {
82  
-        rtc.fire('disconnect stream', rtc._socket.id);
83  
-        delete rtc.peerConnections[rtc._socket.id];
84  
-      };
85  
-
86  
-      rtc.on('get_peers', function(data) {
87  
-        rtc.connections = data.connections;
88  
-        // fire connections event and pass peers
89  
-        rtc.fire('connections', rtc.connections);
90  
-      });
91  
-
92  
-      rtc.on('receive_ice_candidate', function(data) {
93  
-        var candidate = new IceCandidate(data.label, data.candidate);
94  
-        rtc.peerConnections[data.socketId].processIceMessage(candidate);
95  
-        
96  
-        rtc.fire('receive ice candidate', candidate);
97  
-      });
98  
-
99  
-      rtc.on('new_peer_connected', function(data) {
100  
-        rtc.connections.push(data.socketId);
101  
-
102  
-        var pc = rtc.createPeerConnection(data.socketId);
103  
-        for (var i = 0; i < rtc.streams.length; i++) {
104  
-          var stream = rtc.streams[i];
105  
-          pc.addStream(stream);
106  
-        }
107  
-      });
108  
-
109  
-      rtc.on('remove_peer_connected', function(data) {  
110  
-        rtc.fire('disconnect stream', data.socketId);
111  
-        delete rtc.peerConnections[data.socketId];
112  
-      });
113  
-
114  
-      rtc.on('receive_offer', function(data) {
115  
-        rtc.receiveOffer(data.socketId, data.sdp);
116  
-        rtc.fire('receive offer', data);
117  
-      });
118  
-
119  
-      rtc.on('receive_answer', function(data) {
120  
-        rtc.receiveAnswer(data.socketId, data.sdp);
121  
-        rtc.fire('receive answer', data);
122  
-      });
123  
-
124  
-      rtc.fire('connect');
125  
-    };
126  
-  };
127  
-
128  
-
129  
-  rtc.sendOffers = function() {
130  
-    for (var i = 0, len = rtc.connections.length; i < len; i++) {
131  
-      var socketId = rtc.connections[i];
132  
-      rtc.sendOffer(socketId);
133  
-    }
134  
-  }
135  
-
136  
-  rtc.onClose = function(data) {
137  
-    rtc.on('close_stream', function() {
138  
-      rtc.fire('close_stream', data);
139  
-    });
140  
-  }
141  
-
142  
-  rtc.createPeerConnections = function() {
143  
-    for (var i = 0; i < rtc.connections.length; i++) {
144  
-      rtc.createPeerConnection(rtc.connections[i]);
145  
-    }
146  
-  };
147  
-
148  
-  rtc.createPeerConnection = function(id) {
149  
-    console.log('createPeerConnection');
150  
-    var pc = rtc.peerConnections[id] = new PeerConnection(rtc.SERVER, function(candidate, moreToFollow) {
151  
-      if (candidate) {
152  
-        rtc._socket.send(JSON.stringify({
153  
-          "eventName": "send_ice_candidate",
154  
-          "label": candidate.label,
155  
-          "candidate": candidate.toSdp(),
156  
-          "socketId": id
157  
-        }), function(error){
158  
-          if(error){console.log(error);}
159  
-        });
160  
-      }
161  
-      rtc.fire('ice candidate', candidate, moreToFollow);
162  
-    });
163  
-
164  
-    pc.onopen = function() {
165  
-      // TODO: Finalize this API
166  
-      rtc.fire('peer connection opened');
167  
-    };
168  
-
169  
-    pc.onaddstream = function(event) {
170  
-      // TODO: Finalize this API
171  
-      rtc.fire('add remote stream', event.stream, id);
172  
-    };
173  
-    return pc;
174  
-  };
175  
-
176  
-  rtc.sendOffer = function(socketId) {
177  
-    var pc = rtc.peerConnections[socketId];
178  
-    // TODO: Abstract away video: true, audio: true for offers
179  
-    var offer = pc.createOffer({
180  
-      video: true,
181  
-      audio: true
182  
-    });
183  
-    pc.setLocalDescription(pc.SDP_OFFER, offer);
184  
-    rtc._socket.send(JSON.stringify({
185  
-      "eventName": "send_offer",
186  
-      "socketId": socketId,
187  
-      "sdp": offer.toSdp()
188  
-    }), function(error){
189  
-          if(error){console.log(error);}
190  
-        });
191  
-    pc.startIce();
192  
-  };
193  
-
194  
-
195  
-  rtc.receiveOffer = function(socketId, sdp) {
196  
-    var pc = rtc.peerConnections[socketId];
197  
-    pc.setRemoteDescription(pc.SDP_OFFER, new SessionDescription(sdp));
198  
-    rtc.sendAnswer(socketId);
199  
-  };
200  
-
201  
-
202  
-  rtc.sendAnswer = function(socketId) {
203  
-    var pc = rtc.peerConnections[socketId];
204  
-    var offer = pc.remoteDescription;
205  
-    // TODO: Abstract away video: true, audio: true for answers
206  
-    var answer = pc.createAnswer(offer.toSdp(), {
207  
-      video: true,
208  
-      audio: true
209  
-    });
210  
-    pc.setLocalDescription(pc.SDP_ANSWER, answer);
211  
-    rtc._socket.send(JSON.stringify({
212  
-      "eventName": "send_answer",
213  
-      "socketId": socketId,
214  
-      "sdp": answer.toSdp()
215  
-    }), function(error){
216  
-          if(error){console.log(error);}
217  
-        });
218  
-    pc.startIce();
219  
-  };
220  
-
221  
-
222  
-  rtc.receiveAnswer = function(socketId, sdp) {
223  
-    var pc = rtc.peerConnections[socketId];
224  
-    pc.setRemoteDescription(pc.SDP_ANSWER, new SessionDescription(sdp));
225  
-  };
226  
-
227  
-
228  
-  rtc.createStream = function(opt, onSuccess, onFail) {
229  
-    var options;
230  
-    onSuccess = onSuccess ||
231  
-    function() {};
232  
-    onFail = onFail ||
233  
-    function() {};
234  
-
235  
-    if(opt.audio && opt.video){
236  
-      options = {
237  
-        video: true,
238  
-        audio: true
239  
-      };
240  
-    }else if(opt.video){
241  
-      options = {
242  
-        video: true,
243  
-        audio: false
244  
-      };
245  
-    }else if(opt.audio){
246  
-      options = {
247  
-        video: false,
248  
-        audio: true
249  
-      };
250  
-    }else {
251  
-      options = {
252  
-        video: false,
253  
-        audio: false
254  
-      };
255  
-    }
256  
-
257  
-    if (getUserMedia) {
258  
-      rtc.numStreams++;
259  
-      getUserMedia.call(navigator, options, function(stream) {
260  
-        
261  
-        rtc.streams.push(stream);
262  
-        rtc.initializedStreams++;
263  
-        onSuccess(stream);
264  
-        if (rtc.initializedStreams === rtc.numStreams) {
265  
-          rtc.fire('ready');
266  
-        }
267  
-      }, function() {
268  
-        alert("Could not connect stream.");
269  
-        onFail();
270  
-      });
271  
-    } else {
272  
-      alert('webRTC is not yet supported in this browser.');
273  
-    }
274  
-  }
275  
-
276  
-
277  
-  rtc.addStreams = function() {
278  
-    for (var i = 0; i < rtc.streams.length; i++) {
279  
-      var stream = rtc.streams[i];
280  
-      for (var connection in rtc.peerConnections) {
281  
-        rtc.peerConnections[connection].addStream(stream);
282  
-      }
283  
-    }
284  
-  };
285  
-
286  
-
287  
-  rtc.attachStream = function(stream, domId) {
288  
-    document.getElementById(domId).src = URL.createObjectURL(stream);
289  
-  };
290  
-
291  
-  rtc.on('ready', function() {
292  
-    rtc.createPeerConnections();
293  
-    rtc.addStreams();
294  
-    rtc.sendOffers();
295  
-  });
296  
-
297  
-}).call(this);
1  example/webrtc.io.js

0 notes on commit 04fd812

Please sign in to comment.
Something went wrong with that request. Please try again.