-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.html
137 lines (126 loc) · 4.91 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous">
</head>
<body>
<div class="container" id="app">
<div class="col-md-4 col-lg-6 offset-lg-3 offset-md-4">
<div v-if="ready">
<h4>{{name}}</h4>
<p v-for="item in info">
<small>{{item.name}} {{item.type}}</small>
</p>
</div>
<form @submit.prevent="setName" class="mt-4" v-else>
<div class="form-group row">
<label>Set Your Name First</label>
<input type="text" class="form-control col-9" v-model="name" placeholder="Type Here">
<input type="submit" value="Add" class="btn btn-sm btn-info ml-1 col-2">
</div>
</form>
<div class="card bg-info" v-if="ready">
<div class="card-header text-white">
Alladin Chats
<span class="float-right">{{connectionCount}} connections, {{online.length}} Online</span>
</div>
<ul class="list-group list-group-flush text-right">
<small v-if="typing" class="text-white">
<i>{{typing}} is typing</i>
</small>
<li class="list-group-item" v-for="message in messages">
<span :class="{'float-left':(message.type===1)}">
{{message.message}}
<small>:{{message.by}}</small>
</span>
</li>
</ul>
<div class="card-body">
<form @submit.prevent="send">
<div class="form-group">
<input type="text" class="form-control" v-model="newmessage" placeholder="Type Here">
</div>
</form>
</div>
</div>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script>
var socket = io();
let app = new Vue({
el: '#app',
data: {
newmessage: null,
messages: [],
typing: false,
online: [],
name: null,
ready: false,
info: [],
connectionCount: 0
},
methods: {
send() {
socket.emit('chat-message', { message: this.newmessage, user: this.name })
this.messages.push({ message: this.newmessage, type: 0, by: 'Me' })
this.newmessage = null
},
isTyping() {
socket.emit('typing', this.name)
},
setName() {
socket.emit('joined', this.name)
this.ready = true
}
},
mounted() {
window.onbeforeunload = () => {
socket.emit('leaved', this.name)
}
socket.on('noOfConnections', (count) => {
this.connectionCount = count
})
},
watch: {
newmessage(value) {
value ? socket.emit('typing', this.name) : socket.emit('stoptyping')
}
},
created() {
socket.on('chat-message', (data) => {
this.messages.push({ message: data.message, type: 1, by: data.user })
this.typing = false
})
socket.on('typing', (data) => {
console.log(data)
this.typing = data
})
socket.on('stoptyping', () => {
this.typing = false
})
socket.on('leaved', (name) => {
this.online.splice(this.online.indexOf(name))
this.info.push({ name: name, type: 'Leaved' })
setTimeout(() => {
this.info = []
}, 5000);
})
socket.on('joined', (name) => {
this.online.push(name)
this.info.push({ name: name, type: 'Joined' })
setTimeout(() => {
this.info = []
}, 5000);
})
}
});
</script>
</body>
</html>