-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
91 lines (81 loc) · 2.83 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var Jingle = require('jingle')
, attachMediaStream = require('attachmediastream')
var socket = new Primus('https://xmpp-ftw.herokuapp.com')
var jingle = new Jingle()
var loginInfo = document.getElementById('loginInfo')
var localStarted = false
loginInfo.onsubmit = function (e) {
if (e.preventDefault) e.preventDefault()
var jid = document.getElementById('jid').value
var username = jid.slice(0, jid.indexOf('@'))
console.log('Connected')
socket.send(
'xmpp.login', {
jid: jid,
password: document.getElementById('password').value,
host: document.getElementById('host').value,
resource: document.getElementById('resource').value || 'jingle-rocks'
}
)
socket.on('xmpp.connection', function(data) {
console.log('connected', data)
socket.send('xmpp.presence', {})
document.getElementById('myJID').textContent = data.jid.user +
'@' + data.jid.domain + '/' + data.jid.resource
})
jingle.on('incoming', function (session) {
console.log('incoming session', session)
session.accept()
})
jingle.on('peerStreamAdded', function(session) {
console.log('peerStreamAdded', session)
attachMediaStream(session.stream, document.getElementById('remoteVideo'))
})
jingle.on('localStream', function (stream) {
if (false === localStarted) {
attachMediaStream(stream, document.getElementById('localVideo'), { muted: true, mirror: true })
localStarted = true
}
})
jingle.on('send', function(data) {
if (data.jingle && (data.jingle.action == 'session-accept')) {
console.debug('sending', data)
window.jingleAccept = data
}
socket.send('xmpp.jingle.request', data, function(error, success) {
if (error) return console.error('Failed', error)
console.log(data.jingle.action + ' ack', success)
})
})
var callInfo = document.getElementById('callInfo')
callInfo.onsubmit = function (e) {
e.preventDefault()
var jid = document.getElementById('peer').value
jingle.startLocalMedia(null, function (error, stream) {
localStarted = true
var sess = jingle.createMediaSession(jid)
sess.start()
console.log('Calling ' + jid)
})
return false
}
return false
}
socket.on('xmpp.error.client', function(error) {
console.error(error)
})
jingle.startLocalMedia(null, function (error, stream) {
if (error) return console.error(error)
attachMediaStream(stream, document.getElementById('localVideo'), { muted: true, mirror: true })
localStarted = true
})
socket.on('xmpp.jingle.request', function(data) {
if (false === localStarted) {
jingle.startLocalMedia(null, function (error, stream) {
if (error) return console.error(error)
attachMediaStream(stream, document.getElementById('localVideo'), { muted: true, mirror: true })
})
localStarted = true
}
jingle.process(data)
})