-
Notifications
You must be signed in to change notification settings - Fork 8
/
chat.php
78 lines (47 loc) · 1.26 KB
/
chat.php
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
<!DOCTYPE html>
<html>
<head>
<base href="http://localhost:8888/PHP-MySQL-Sockets-Chat/">
<link rel="stylesheet" href="bootstrap/css/bootstrap.css" />
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<div class="container">
<div class="chat"></div>
<form id="message-form">
<input type="text" name="message" id="message" placeholder="Chat here..." />
<button name="send" id="send" class="btn">Send</button>
</form>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e){
console.log("Connection established!");
};
conn.onmessage = function(e){
addMessage(e.data);
};
$(function(){
$('#message-form').submit(function(e){
e.preventDefault();
var el = $('#message');
var message = el.val();
var messageData = {
'id': '123',
'userId': '12334',
'content': 'messagehere'
}
var messageDataJson = JSON.stringify(messageData);
conn.send(messageDataJson);
addMessage(message);
el.val('');
});
});
function addMessage(message)
{
$('.chat').append('<div class="chat-message">' + message + '</div>');
}
</script>
</div>
</body>
</html>