1 | Call = require './Call'
|
2 | shims = require './shims'
|
3 | ProtoSock = require 'protosock'
|
4 |
|
5 | client =
|
6 | options:
|
7 | namespace: 'holla'
|
8 | resource: 'default'
|
9 | debug: false
|
10 |
|
11 | register: (name, cb) ->
|
12 | @ssocket.write
|
13 | type: "register"
|
14 | args:
|
15 | name: name
|
16 |
|
17 | @once "register", (worked) =>
|
18 | if worked
|
19 | @user = name
|
20 | @emit "authorized"
|
21 | @authorized = worked
|
22 | cb? worked
|
23 |
|
24 | call: (user) -> new Call @, user, true
|
25 | chat: (user, msg) ->
|
26 | @ssocket.write
|
27 | type: "chat"
|
28 | to: user
|
29 | args:
|
30 | message: msg
|
31 | return @
|
32 |
|
33 | ready: (fn) ->
|
34 | if @authorized
|
35 | fn()
|
36 | else
|
37 | @once 'authorized', fn
|
38 | return @
|
39 |
|
40 | validate: (socket, msg, done) ->
|
41 | if @options.debug
|
42 | console.log msg
|
43 | return done false unless typeof msg is 'object'
|
44 | return done false unless typeof msg.type is 'string'
|
45 | if msg.type is "register"
|
46 | return done false unless typeof msg.args is 'object'
|
47 | return done false unless typeof msg.args.result is 'boolean'
|
48 | else if msg.type is "offer"
|
49 | return done false unless typeof msg.from is 'string'
|
50 | else if msg.type is "answer"
|
51 | return done false unless typeof msg.args is 'object'
|
52 | return done false unless typeof msg.from is 'string'
|
53 | return done false unless typeof msg.args.accepted is 'boolean'
|
54 | else if msg.type is "sdp"
|
55 | return done false unless typeof msg.args is 'object'
|
56 | return done false unless typeof msg.from is 'string'
|
57 | return done false unless msg.args.sdp
|
58 | return done false unless msg.args.type
|
59 | else if msg.type is "candidate"
|
60 | return done false unless typeof msg.args is 'object'
|
61 | return done false unless typeof msg.from is 'string'
|
62 | return done false unless typeof msg.args.candidate is 'object'
|
63 | else if msg.type is "chat"
|
64 | return done false unless typeof msg.args is 'object'
|
65 | return done false unless typeof msg.from is 'string'
|
66 | return done false unless typeof msg.args.message is 'string'
|
67 | else if msg.type is "hangup"
|
68 | return done false unless typeof msg.from is 'string'
|
69 | else if msg.type is "presence"
|
70 | return done false unless typeof msg.args is 'object'
|
71 | return done false unless typeof msg.args.name is 'string'
|
72 | return done false unless typeof msg.args.online is 'boolean'
|
73 | else
|
74 | return done false
|
75 | return done true
|
76 |
|
77 | error: (socket, err) -> @emit 'error', err, socket
|
78 | message: (socket, msg) ->
|
79 | switch msg.type
|
80 | when "register"
|
81 | @emit "register", msg.args.result
|
82 |
|
83 | when "offer"
|
84 | c = new Call @, msg.from, false
|
85 | @emit "call", c
|
86 |
|
87 | when "presence"
|
88 | @emit "presence", msg.args
|
89 | @emit "presence.#{msg.args.name}", msg.args.online
|
90 |
|
91 | when "chat"
|
92 | @emit "chat", {from: msg.from, message: msg.args.message}
|
93 | @emit "chat.#{msg.from}", msg.args.message
|
94 |
|
95 | when "hangup"
|
96 | @emit "hangup", {from: msg.from}
|
97 | @emit "hangup.#{msg.from}"
|
98 |
|
99 | when "answer"
|
100 | @emit "answer", {from: msg.from, accepted: msg.args.accepted}
|
101 | @emit "answer.#{msg.from}", msg.args.accepted
|
102 |
|
103 | when "candidate"
|
104 | @emit "candidate", {from: msg.from, candidate: msg.args.candidate}
|
105 | @emit "candidate.#{msg.from}", msg.args.candidate
|
106 |
|
107 | when "sdp"
|
108 | @emit "sdp", {from: msg.from, sdp: msg.args.sdp, type: msg.args.type}
|
109 | @emit "sdp.#{msg.from}", msg.args
|
110 |
|
111 |
|
112 | holla =
|
113 | createClient: ProtoSock.createClientWrapper client
|
114 | Call: Call
|
115 | supported: shims.supported
|
116 | config: shims.PeerConnConfig
|
117 | streamToBlob: (s) -> shims.URL.createObjectURL s
|
118 | pipe: (stream, el) ->
|
119 | uri = holla.streamToBlob stream
|
120 | shims.attachStream uri, el
|
121 |
|
122 | record: shims.recordVideo
|
123 |
|
124 | createStream: (opt, cb) ->
|
125 | return cb "Missing getUserMedia" unless shims.getUserMedia?
|
126 | err = cb
|
127 | succ = (s) -> cb null, s
|
128 | shims.getUserMedia opt, succ, err
|
129 | return holla
|
130 |
|
131 | createFullStream: (cb) -> holla.createStream {video:true,audio:true}, cb
|
132 | createVideoStream: (cb) -> holla.createStream {video:true,audio:false}, cb
|
133 | createAudioStream: (cb) -> holla.createStream {video:false,audio:true}, cb
|
134 |
|
135 | module.exports = holla |
\ | No newline at end of file |