-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbully old.html
127 lines (106 loc) · 3.11 KB
/
bully old.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="sockjs-0.3.js"></script>
<script src="stomp.js"></script>
<style>
.box {
width: 440px;
float: left;
margin: 0 20px 0 20px;
}
.box div, .box input {
border: 1px solid;
-moz-border-radius: 4px;
border-radius: 4px;
width: 100%;
padding: 5px;
margin: 3px 0 10px 0;
}
.box div {
border-color: grey;
height: 300px;
overflow: auto;
}
div code {
display: block;
}
#first div code {
-moz-border-radius: 2px;
border-radius: 2px;
border: 1px solid #eee;
margin-bottom: 5px;
}
#second div {
font-size: 0.8em;
}
</style>
<title>Bully's Algorithm over Browsers</title>
<link href="main.css" rel="stylesheet" type="text/css"/>
</head>
<body lang="en">
<h1>Bully's Algorithm over Browser</h1>
<div class="box">
<input id="pid" placeholder="Set your PID"></input>
<input type="button" value="connect" onclick="connect()"/>
<input type="button" value="disconnect" onclick="disconnect()"></input>
</div>
<div id="first" class="box">
<h2>Received</h2>
<div id="received"></div>
<form><input autocomplete="off" value="Type here..."></input></form>
</div>
<div id="second" class="box">
<h2>Logs</h2>
<div id="logs"></div>
</div>
<script type="text/javascript">
msgCtr = 0;
pid = -1;
function print(elId, text) {
if (elId != null) {
var div = document.getElementById(elId);
div.innerHTML += "<br/>" + text;
div.scrollTop = div.scrollHeight;;
}
}
function onConnect() {
pid = document.getElementById('pid').value
print('logs', 'Connected. PID = ' + pid);
topic = "/topic/test";
subscription = client.subscribe(topic, onMessage);
sendMsg();
}
function onDisconnect() {
print('logs', 'Disconnected');
}
function onError(error) {
print('logs', 'Error')
print('logs', 'error: ' + error);
}
function connect() {
print('logs', 'Connecting...');
// client.connect('ap0n', 'ap0n123', onConnect, onError, '/');
// ws = new SockJS('http://147.27.14.117:15674/stomp');
ws = new SockJS('http://192.168.178.4:15674/stomp');
client = Stomp.over(ws);
client.heartbeat.outgoing = 0;
client.heartbeat.incoming = 0;
client.connect('ap0n', 'ap0n123', onConnect, onError, '/');
}
function disconnect() {
client.disconnect(onDisconnect);
}
function onMessage(receivedMessage) {
if (String(receivedMessage.body).substring(0, 3) !== '[' + pid + ']') {
print('received', receivedMessage.body);
setTimeout(sendMsg, 500);
}
}
function sendMsg() {
var msg = "[" + pid + "]"+ ": " + (msgCtr++);
print('logs', "sending: " + msg);
client.send(topic, {"content-type":"text/plain"}, msg);
}
</script>
</body></html>