-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
52 lines (44 loc) · 1.11 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
<?php
session_start();
if(isset($_POST['submit'])){
$name=$_POST['name'];
$_SESSION['name']=$name;
}
?>
<?php
if(!isset($_SESSION['name'])){
?>
<form method="post">
<label>Enter Your Name: <b style="color: black;"></b></label>
<input type="text" name="name" required>
<input type="submit" name="submit" value="Submit">
</form>
<?php } else { ?>
<input type="text" id="msg">
<input type="button" id="btn" value="Send">
<div id="msg_box"></div>
<script src="https://code.jquery.com/jquery-3.6.0.js" ></script>
<script>
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
var getData=jQuery.parseJSON(e.data);
var html="<b>"+getData.name+"</b>: "+getData.msg+"<br/>";
jQuery('#msg_box').append(html);
};
jQuery('#btn').click(function(){
var msg=jQuery('#msg').val();
var name="<?php echo $_SESSION['name']?>";
var content={
msg:msg,
name:name
};
conn.send(JSON.stringify(content));
var html="<b>"+name+"</b>: "+msg+"<br/>";
jQuery('#msg_box').append(html);
jQuery('#msg').val('');
});
</script>
<?php } ?>