working websocket server
This commit is contained in:
parent
d5f0b09fba
commit
a1106c4b22
@ -4,7 +4,7 @@
|
||||
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
|
||||
<script>
|
||||
let socket = new WebSocket("ws://localhost:5000");
|
||||
let projectID = "1234"; // hier project ID eintragen
|
||||
let projectID = "einTollesProjekt"; // hier project ID eintragen
|
||||
|
||||
function setVariable(name, value) {
|
||||
console.log(`Setting variable: ${name} = ${value}`);
|
||||
@ -25,10 +25,11 @@
|
||||
user: "testClient"+Math.random()
|
||||
}));
|
||||
|
||||
setInterval(function() { setVariable('test',Math.random()) }, Math.random(3)*1000);
|
||||
setInterval(function() { setVariable('test','einTollerText'+Math.random()) }, 1000);
|
||||
};
|
||||
|
||||
socket.onmessage = function(event) {
|
||||
console.log(event.data);
|
||||
for (const message of event.data.split("\n")) {
|
||||
console.log(`${message}`);
|
||||
const obj = JSON.parse(message);
|
||||
|
56
docs/other.html
Normal file
56
docs/other.html
Normal file
@ -0,0 +1,56 @@
|
||||
<html>
|
||||
<body>
|
||||
<div id="data"></div>
|
||||
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
|
||||
<script>
|
||||
let socket = new WebSocket("ws://localhost:5000");
|
||||
let projectID = "5678"; // hier project ID eintragen
|
||||
|
||||
function setVariable(name, value) {
|
||||
console.log(`Setting variable: ${name} = ${value}`);
|
||||
socket.send(JSON.stringify({
|
||||
method: "set",
|
||||
project_id: projectID,
|
||||
name,
|
||||
value
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
socket.onopen = function(e) {
|
||||
console.log("connection established");
|
||||
socket.send(JSON.stringify({
|
||||
method: "handshake",
|
||||
project_id: projectID,
|
||||
user: "testClient"+Math.random()
|
||||
}));
|
||||
|
||||
setInterval(function() { setVariable('test',666) }, 100);
|
||||
};
|
||||
|
||||
socket.onmessage = function(event) {
|
||||
console.log(event.data);
|
||||
for (const message of event.data.split("\n")) {
|
||||
console.log(`${message}`);
|
||||
const obj = JSON.parse(message);
|
||||
if (obj.method === "set") {
|
||||
// variable name in obj.name, Wert der Variablen steht in obj.value
|
||||
$('#data').html(`<b/>${obj.name}:</b> <pre>${obj.value}</pre>`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
socket.onclose = function(event) {
|
||||
if (event.wasClean) {
|
||||
console.log(`connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||
} else {
|
||||
console.log('connection died');
|
||||
}
|
||||
};
|
||||
|
||||
socket.onerror = function(error) {
|
||||
console.log(`[error] ${error.data}`);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
43
docs/receive.html
Normal file
43
docs/receive.html
Normal file
@ -0,0 +1,43 @@
|
||||
<html>
|
||||
<body>
|
||||
<div id="data"></div>
|
||||
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
|
||||
<script>
|
||||
let socket = new WebSocket("ws://localhost:5000");
|
||||
let projectID = "einTollesProjekt"; // hier project ID eintragen
|
||||
|
||||
socket.onopen = function(e) {
|
||||
console.log("connection established");
|
||||
socket.send(JSON.stringify({
|
||||
method: "handshake",
|
||||
project_id: projectID,
|
||||
user: "testClient"+Math.random()
|
||||
}));
|
||||
};
|
||||
|
||||
socket.onmessage = function(event) {
|
||||
console.log(event.data);
|
||||
for (const message of event.data.split("\n")) {
|
||||
console.log(`${message}`);
|
||||
const obj = JSON.parse(message);
|
||||
if (obj.method === "set") {
|
||||
// variable name in obj.name, Wert der Variablen steht in obj.value
|
||||
$('#data').html(`<b/>${obj.name}:</b> <pre>${obj.value}</pre>`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
socket.onclose = function(event) {
|
||||
if (event.wasClean) {
|
||||
console.log(`connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||
} else {
|
||||
console.log('connection died');
|
||||
}
|
||||
};
|
||||
|
||||
socket.onerror = function(error) {
|
||||
console.log(`[error] ${error.data}`);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
7
test.py
7
test.py
@ -14,14 +14,14 @@ async def handshake(ws,j):
|
||||
async def set_variable(j):
|
||||
for name,ws in clients.get(j.get('project_id'),{}).items():
|
||||
print(f"> {name} - {json.dumps(j)}")
|
||||
ws.send(json.dumps(j))
|
||||
await ws.send(json.dumps(j))
|
||||
|
||||
async def disconnect(project_id,user):
|
||||
print("disconnecting",user)
|
||||
clients.get(project_id,[]).pop(user)
|
||||
|
||||
|
||||
async def hello(websocket, path):
|
||||
async def process(websocket, path):
|
||||
project_id = ""
|
||||
user = ""
|
||||
async for data in websocket:
|
||||
@ -36,7 +36,6 @@ async def hello(websocket, path):
|
||||
pass
|
||||
await disconnect(project_id,user)
|
||||
|
||||
start_server = websockets.serve(hello, 'localhost', 5000)
|
||||
|
||||
start_server = websockets.serve(process, 'localhost', 5000, compression=None)
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
Loading…
x
Reference in New Issue
Block a user