-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
executable file
·66 lines (59 loc) · 2.39 KB
/
index.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
<html>
<head>
<style>
body{width:600px;font-family:calibri;}
.error {color:#FF0000;}
.chat-connection-ack{color: #26af26;}
.chat-message {border-bottom-left-radius: 4px;border-bottom-right-radius: 4px;
}
#btnSend {background: #26af26;border: #26af26 1px solid; border-radius: 4px;color: #FFF;display: block;margin: 15px 0px;padding: 10px 50px;cursor: pointer;
}
#chat-box {background: #fff8f8;border: 1px solid #ffdddd;border-radius: 4px;border-bottom-left-radius:0px;border-bottom-right-radius: 0px;min-height: 300px;padding: 10px;overflow: auto;
}
.chat-box-html{color: #09F;margin: 10px 0px;font-size:0.8em;}
.chat-box-message{color: #09F;padding: 5px 10px; background-color: #fff;border: 1px solid #ffdddd;border-radius:4px;display:inline-block;}
.chat-input{border: 1px solid #ffdddd;border-top: 0px;width: 100%;box-sizing: border-box;padding: 10px 8px;color: #191919;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function showMessage(messageHTML) {
$('#chat-box').append(messageHTML);
}
$(document).ready(function(){
var websocket = new WebSocket("ws://localhost:8090/demo/php-socket.php");
websocket.onopen = function(event) {
showMessage("<div class='chat-connection-ack'>Connection is established!</div>");
}
websocket.onmessage = function(event) {
var Data = JSON.parse(event.data);
showMessage("<div class='"+Data.message_type+"'>"+Data.message+"</div>");
$('#chat-message').val('');
};
websocket.onerror = function(event){
showMessage("<div class='error'>Problem due to some Error</div>");
};
websocket.onclose = function(event){
showMessage("<div class='chat-connection-ack'>Connection Closed</div>");
};
$('#frmChat').on("submit",function(event){
event.preventDefault();
$('#chat-user').attr("type","hidden");
var messageJSON = {
chat_user: $('#chat-user').val(),
chat_message: $('#chat-message').val()
};
websocket.send(JSON.stringify(messageJSON));
});
});
</script>
</head>
<body>
<form name="frmChat" id="frmChat">
<div id="chat-box"></div>
<input type="text" name="chat-user" id="chat-user" placeholder="Name" class="chat-input" required />
<input type="text" name="chat-message" id="chat-message" placeholder="Message" class="chat-input chat-message" required />
<input type="submit" id="btnSend" name="send-chat-message" value="Send" >
</form>
</body>
</html>