let socket = new WebSocket("wss://javascript.info/article/websocket/demo/hello"); | |||||
socket.onopen = function (e) { | |||||
alert("[open] Connection established"); | |||||
alert("Sending to server"); | |||||
socket.send("My name is John"); | |||||
}; | |||||
socket.onmessage = function (event) { | |||||
alert(`[message] Data received from server: ${event.data}`); | |||||
}; | |||||
socket.onclose = function (event) { | |||||
if (event.wasClean) { | |||||
alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); | |||||
} else { | |||||
// e.g. server process killed or network down | |||||
// event.code is usually 1006 in this case | |||||
alert('[close] Connection died'); | |||||
} | |||||
}; | |||||
socket.onerror = function (error) { | |||||
alert(`[error] ${error.message}`); | |||||
}; |
<input type="submit" value="Go!"> | <input type="submit" value="Go!"> | ||||
</form> | </form> | ||||
<th>Bezeichnung</th> | |||||
<th>Kaufort</th> | |||||
<th>Anz</th> | |||||
<th>E-Preis</th> | |||||
<th>G-Preis</th> | |||||
<table class="table"> | <table class="table"> | ||||
<th>Bezeichnung</th> | |||||
<th>Kaufort</th> | |||||
<th>Anz</th> | |||||
<th>E-Preis</th> | |||||
<th>G-Preis</th> | |||||
<script type="text/javascript"> | <script type="text/javascript"> | ||||
//var mydata = JSON.parse(stadtbezirk); | //var mydata = JSON.parse(stadtbezirk); | ||||
var sumpreis = 0; | var sumpreis = 0; |
|
| |||||
const http = require('http'); | |||||
const ws = require('ws'); | |||||
const wss = new ws.Server({ noServer: true }); | |||||
function accept(req, res) { | |||||
// all incoming requests must be websockets | |||||
if (!req.headers.upgrade || req.headers.upgrade.toLowerCase() != 'websocket') { | |||||
res.end(); | |||||
return; | |||||
} | |||||
// can be Connection: keep-alive, Upgrade | |||||
if (!req.headers.connection.match(/\bupgrade\b/i)) { | |||||
res.end(); | |||||
return; | |||||
} | |||||
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onConnect); | |||||
} | |||||
function onConnect(ws) { | |||||
ws.on('message', function (message) { | |||||
let name = message.match(/([\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]+)$/gu) || "Guest"; | |||||
ws.send(`Hello from server, ${name}!`); | |||||
setTimeout(() => ws.close(1000, "Bye!"), 50000); | |||||
}); | |||||
} | |||||
if (!module.parent) { | |||||
http.createServer(accept).listen(8080); | |||||
} else { | |||||
exports.accept = accept; | |||||
} |