-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (71 loc) · 2.32 KB
/
index.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
// console.log("reached js file")
const wsLOCALHOST = `ws://localhost:8888/ws?user=`
let myApp = {}
myApp.websocket
myApp.container
myApp.append = (msg) => {
let p = document.createElement('p')
p.innerHTML = msg
myApp.container.append(p)
var elem = document.getElementById("container")
elem.scrollTop = elem.scrollHeight
}
/**
* 1. Sets the user window message as "me: msg"
* 2. Sends the msg to server
*/
myApp.sendmsg = () => {
let msg = document.getElementById('user-input').value
console.log(msg)
// maybe better to append after ws send
myApp.append(`<b>me</b>: ${msg}`)
console.log("after send msg")
myApp.websocket.send(JSON.stringify({
Msg: msg
}))
document.getElementById('user-input').value = ''
}
myApp.init = () => {
let user = prompt(`What's your name?`)
myApp.websocket = new WebSocket(`${wsLOCALHOST}${user}`)
console.log(myApp.websocket)
myApp.container = document.getElementById('container')
myApp.websocket.onmessage = (event) => {
let msg = ''
console.log("received onmsg")
let res = JSON.parse(event.data)
console.log("reached here...")
switch(res.Option) {
case 'connect':
msg = `<b>${res.User}</b> has joined the room.`
break
case 'disconnect':
msg = `<b>${res.User}</b> has left the room.`
break
default:
console.log(res.Option);
msg = `<b>${res.User}</b>: ${res.Message}`
break
}
myApp.append(msg)
}
}
const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
}
}
toggleSwitch.addEventListener('change', switchTheme, false);
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
window.onload = myApp.init