This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpage.js
155 lines (131 loc) · 4.48 KB
/
page.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/** The core Vue UI */
const vm = new Vue({
el: '#vue-instance',
data() {
return {
ZentalkWorker: null,
socket: null,
originPublicKey: null,
destinationPublicKey: null,
messages: [],
notifications: [],
currentRoom: null,
pendingRoom: Math.floor(Math.random() * 1000 * 100),
draft: '',
address: null
}
},
async created() {
this.addNotification('Welcome to Zentalk-Web!')
this.addNotification('Please Wait Zentalk Generating New Key-Pair...')
this.ZentalkWorker = new Worker('zentalk-worker.js')
this.originPublicKey = await this.getWebWorkerResponse('generate-keys')
this.addNotification(`Zentalk: Keypairs Are Now Generated: ${this.getKeySnippet(this.originPublicKey)}`)
this.socket = io()
this.setupSocketListeners()
},
methods: {
setupSocketListeners() {
this.socket.on('connect', () => {
this.addNotification('You Are Now Connected With Zentalk')
this.joinRoom()
})
this.socket.on('disconnect', () => this.addNotification('Lost Connection'))
this.socket.on('MESSAGE', async (message) => {
if (message.recipient === this.originPublicKey) {
message.text = await this.getWebWorkerResponse('decrypt', message.text)
this.messages.push(message)
}
})
this.socket.on('NEW_CONNECTION', () => {
this.addNotification('Another User Has Joined The Room')
this.sendPublicKey()
})
this.socket.on('ROOM_JOINED', (newRoom) => {
this.currentRoom = newRoom
this.addNotification(`You Have Joined The Zentaroom - ${this.currentRoom}`)
this.sendPublicKey()
})
this.socket.on('PUBLIC_KEY', (key) => {
this.addNotification(`Public Key Received - ${this.getKeySnippet(key)}`)
this.destinationPublicKey = key
})
this.socket.on('user disconnected', () => {
this.notify(`The User is Disconnected - ${this.getKeySnippet(this.destinationKey)}`)
this.destinationPublicKey = null
})
this.socket.on('ROOM_FULL', () => {
this.addNotification(`Cannot join ${this.pendingRoom}, Zentaroom is full`)
this.pendingRoom = Math.floor(Math.random() * 1000 * 10)
this.joinRoom()
})
this.socket.on('INTRUSION_ATTEMPT', () => {
this.addNotification('A third user are attempted to join the Zentarooms')
})
},
async sendMessage() {
if (!this.draft || this.draft === '') {
return
}
let message = Immutable.Map({
text: this.draft,
recipient: this.destinationPublicKey,
sender: this.originPublicKey
})
this.draft = ''
this.addMessage(message.toObject())
if (this.destinationPublicKey) {
const encryptedText = await this.getWebWorkerResponse(
'encrypt', [message.get('text'), this.destinationPublicKey])
const encryptedMsg = message.set('text', encryptedText)
this.socket.emit('MESSAGE', encryptedMsg.toObject())
}
},
joinRoom() {
if (this.pendingRoom !== this.currentRoom && this.originPublicKey) {
this.addNotification(`Connecting to Zentaroom - ${this.pendingRoom}`)
this.messages = []
this.destinationPublicKey = null
this.socket.emit('JOIN', this.pendingRoom)
}
},
addMessage(message) {
this.messages.push(message)
this.autoscroll(this.$refs.chatContainer)
},
addNotification(message) {
const timestamp = new Date().toLocaleTimeString()
this.notifications.push({
message,
timestamp
})
this.autoscroll(this.$refs.notificationContainer)
},
getWebWorkerResponse(messageType, messagePayload) {
return new Promise((resolve, reject) => {
const messageId = Math.floor(Math.random() * 100000 * 10)
this.ZentalkWorker.postMessage([messageType, messageId].concat(messagePayload))
const handler = function (e) {
if (e.data[0] === messageId) {
e.currentTarget.removeEventListener(e.type, handler)
resolve(e.data[1])
}
}
this.ZentalkWorker.addEventListener('message', handler)
})
},
sendPublicKey() {
if (this.originPublicKey) {
this.socket.emit('PUBLIC_KEY', this.originPublicKey)
}
},
getKeySnippet(key) {
return key.slice(400, 416)
},
autoscroll(element) {
if (element) {
element.scrollTop = element.scrollHeight
}
}
}
})