Renamed Files Added Connector
This commit is contained in:
parent
c72d936f24
commit
5e0c7ace19
25
Connect.js
Normal file
25
Connect.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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}`);
|
||||||
|
};
|
@ -40,13 +40,13 @@
|
|||||||
<input type="submit" value="Go!">
|
<input type="submit" value="Go!">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
||||||
<table class="table">
|
|
||||||
<th>Bezeichnung</th>
|
<th>Bezeichnung</th>
|
||||||
<th>Kaufort</th>
|
<th>Kaufort</th>
|
||||||
<th>Anz</th>
|
<th>Anz</th>
|
||||||
<th>E-Preis</th>
|
<th>E-Preis</th>
|
||||||
<th>G-Preis</th>
|
<th>G-Preis</th>
|
||||||
|
<table class="table">
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
//var mydata = JSON.parse(stadtbezirk);
|
//var mydata = JSON.parse(stadtbezirk);
|
||||||
var sumpreis = 0;
|
var sumpreis = 0;
|
||||||
|
@ -1 +0,0 @@
|
|||||||
|
|
36
Server.js
Normal file
36
Server.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user