-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.html
44 lines (42 loc) · 1.78 KB
/
view.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
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Websocket</title>
</head>
<body>
<input style="width: 800px;height: 200px" type="text" placeholder="enter text here to send to server" id="eingabe" onkeypress="myFunction(event)"/>
<button onclick="clearlog()">Clear Logs</button>
<div style="overflow:scroll; width: 800px; height: 550px; border: solid; border-radius: 9px" id="display"></div>
<script>
//var sock =new WebSocket("ws://localhost:5001");
var sock =new WebSocket("ws://192.168.1.23:3000"); //replace this address with the one the node.js server prints out. keep port 3000
var display=document.getElementById("display")
sock.onopen=function(event){
//alert("Socket connected succesfully!!!")
setTimeout(function(){sock.send('connection succeeded');},1000);
display.innerHTML+="connection succeeded <br />";
};
sock.onmessage=function(event){
console.log(event);//show received from server data in console of browser
display.innerHTML+=event.data+"<br />"; //add incoming message from server to the log screen previous string + new string(message)
}
function myFunction(event) {
//sock.send("Hello");
if(event.keyCode==13){ //when enter is pressed...
var text=document.getElementById('eingabe').value; //assign value of the entered string to te text variable
if(text!=""){ //if text is not an empty string
//display.innerHTML+="From Client: "+text+"<br />"; //show the message from client in div
sock.send(text); //send string to server
display.innerHTML+="<strong>Me: </strong>" + text+"<br />"; //display input text in div (client side)
document.getElementById('eingabe').value=""; //and clear the input field
}
else{
console.log("empty string not sent")
}
}}
function clearlog(){
display.innerHTML="";
}
</script>
</body>
</html>