-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.html
63 lines (52 loc) · 1.88 KB
/
websocket.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<form id="chat">
name: <input id="name" type="text" name="fname"><br>
message: <input id="message" type="text" name="lname"><br>
<input type="submit">
</form>
<div >
<ul id="messages">
</ul>
</div>
<script>
(function () {
// if user is running mozilla then use it's built-in WebSocket
$( "#chat" ).submit(function( event ) {
let webMessage = {"userId":29,"chatterId":20,"message":"test","isRead":false};
postData("http://localhost:3000/chats", webMessage).then(value => {
console.error(value);
});
// https://love.duet-api.com/chats
event.preventDefault();
});
}());
function postData(url = '', data = {}) {
// Default options are marked with *
return fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'no-cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'omit', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses JSON response into native JavaScript objects
}
</script>
</body>
</html>