|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2022 Moddable Tech, Inc. |
| 3 | + * |
| 4 | + * This file is part of the Moddable SDK. |
| 5 | + * |
| 6 | + * This work is licensed under the |
| 7 | + * Creative Commons Attribution 4.0 International License. |
| 8 | + * To view a copy of this license, visit |
| 9 | + * <http://creativecommons.org/licenses/by/4.0>. |
| 10 | + * or send a letter to Creative Commons, PO Box 1866, |
| 11 | + * Mountain View, CA 94042, USA. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +import {Server as HTTPServer} from "http" |
| 16 | +import {Server as WebsocketsServer} from "websocket" |
| 17 | + |
| 18 | +const indexHTML = ` |
| 19 | +<html> |
| 20 | +<head> |
| 21 | + <meta charset="utf-8"> |
| 22 | + <title>test</title> |
| 23 | +</head> |
| 24 | +<body> |
| 25 | +
|
| 26 | +<script type="module" src="./ws.js"></script> |
| 27 | +</body> |
| 28 | +</html> |
| 29 | +`; |
| 30 | + |
| 31 | +const wsJS = ` |
| 32 | +const url = "ws://" + window.location.hostname + "/ws"; |
| 33 | +const ws = new WebSocket(url); |
| 34 | +
|
| 35 | +ws.onopen = function() { |
| 36 | + console.log("ws open"); |
| 37 | + ws.send(JSON.stringify({"hello": "world"})); |
| 38 | +} |
| 39 | +
|
| 40 | +ws.onclose = function() { |
| 41 | + console.log("ws close"); |
| 42 | +} |
| 43 | +
|
| 44 | +ws.onerror = function() { |
| 45 | + console.log("ws error"); |
| 46 | +} |
| 47 | +
|
| 48 | +ws.onmessage = function(event) { |
| 49 | + const data = event.data; |
| 50 | + console.log("ws message: ", data); |
| 51 | +} |
| 52 | +`; |
| 53 | + |
| 54 | +// WebSockets server without a listener (signaled with port set to null) |
| 55 | +// the http server hands-off incoming connections to this server |
| 56 | +const websockets = new WebsocketsServer({port: null}); |
| 57 | +websockets.callback = function (message, value) { |
| 58 | + switch (message) { |
| 59 | + case WebsocketsServer.connect: |
| 60 | + trace("ws connect\n"); |
| 61 | + break; |
| 62 | + |
| 63 | + case WebsocketsServer.handshake: |
| 64 | + trace("ws handshake\n"); |
| 65 | + break; |
| 66 | + |
| 67 | + case WebsocketsServer.receive: |
| 68 | + trace(`ws message received: ${value}\n`); |
| 69 | + break; |
| 70 | + |
| 71 | + case WebsocketsServer.disconnect: |
| 72 | + trace("ws close\n"); |
| 73 | + break; |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +// HTTP server on port 80 |
| 78 | +const http = new HTTPServer; |
| 79 | +http.callback = function(message, value, etc) { |
| 80 | + if (HTTPServer.status === message) { |
| 81 | + this.path = value; |
| 82 | + |
| 83 | + // request for "/ws" is handed off to the WebSockets server |
| 84 | + if ("/ws" === value) { |
| 85 | + const socket = http.detach(this); |
| 86 | + websockets.attach(socket); |
| 87 | + } |
| 88 | + |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // return "/", "/index.html" and "/ws.js". all other paths are 404 |
| 93 | + if (HTTPServer.prepareResponse === message) { |
| 94 | + if (("/" === this.path) || ("/index.html" === this.path)) |
| 95 | + return {headers: ["Content-type", "text/html"], body: indexHTML}; |
| 96 | + if ("/ws.js" === this.path) |
| 97 | + return {headers: ["Content-type", "text/javascript"], body: wsJS}; |
| 98 | + |
| 99 | + return {status: 404}; |
| 100 | + } |
| 101 | +} |
0 commit comments