Browse Source

code init

221V 1 week ago
commit
0139d0cb71
9 changed files with 228 additions and 0 deletions
  1. 7 0
      README.md
  2. 19 0
      comp.mk
  3. 16 0
      stest/.gitignore
  4. 3 0
      stest/Makefile
  5. 17 0
      stest/dub.json
  6. 6 0
      stest/dub.selections.json
  7. 33 0
      stest/source/app.d
  8. 56 0
      stest/static/index.html
  9. 71 0
      stest/static/style.css

+ 7 - 0
README.md

@@ -0,0 +1,7 @@
+# serverino http + ws server test
+
+```
+https://code.dlang.org/packages/serverino
+https://github.com/trikko/serverino
+
+```

+ 19 - 0
comp.mk

@@ -0,0 +1,19 @@
+
+
+# todo clean
+
+c:
+	dub build --compiler ldc2
+
+c2:
+	dub build --compiler ldc2 --build release --force
+
+run:
+	./stest
+
+
+
+default: run
+
+.PHONY: c c2 run
+

+ 16 - 0
stest/.gitignore

@@ -0,0 +1,16 @@
+.dub
+docs.json
+__dummy.html
+docs/
+./stest
+stest.so
+stest.dylib
+stest.dll
+stest.a
+stest.lib
+stest-test-*
+*.exe
+*.pdb
+*.o
+*.obj
+*.lst

+ 3 - 0
stest/Makefile

@@ -0,0 +1,3 @@
+
+include ../comp.mk
+

+ 17 - 0
stest/dub.json

@@ -0,0 +1,17 @@
+{
+  "name": "stest",
+  "description": "A simple serverino http + ws server application.",
+  
+  "targetName": "stest",
+  "dflags": ["-w", "-O"],
+  "targetType": "executable",
+  "dependencies": {
+    "serverino": "~>0.7.18"
+  },
+  "libs": [],
+  "lflags": [],
+  
+  "license": "proprietary",
+  "copyright": "Copyright © 2025, 221V",
+  "authors": [ "221V" ]
+}

+ 6 - 0
stest/dub.selections.json

@@ -0,0 +1,6 @@
+{
+	"fileVersion": 1,
+	"versions": {
+		"serverino": "0.7.18"
+	}
+}

+ 33 - 0
stest/source/app.d

@@ -0,0 +1,33 @@
+
+import std;
+
+// Docs: https://trikko.github.io/serverino/
+// Tips and tricks: https://github.com/trikko/serverino/wiki/
+// Examples: https://github.com/trikko/serverino/tree/master/examples
+import serverino;
+
+mixin ServerinoMain;
+
+// Accept a new connection only if the request path is "/echo"
+@onWebSocketUpgrade bool onUpgrade(Request req){
+  return req.path == "/echo";
+}
+
+// Handle the WebSocket connection
+@endpoint void echo(Request r, WebSocket ws){
+  // Read messages from the client
+  while(true){
+    // Only if the message is valid
+    if( WebSocketMessage msg = ws.receiveMessage() ){
+      // Send the message back to the client
+      ws.send("I received your message: `" ~ msg.asString ~ "`");
+    }
+  }
+}
+
+@route!"/"
+@endpoint void index(Request r, Output o){ o.serveFile("static/index.html"); }
+
+@route!"/style.css"
+@endpoint void css(Request r, Output o){ o.serveFile("static/style.css"); }
+

+ 56 - 0
stest/static/index.html

@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>WebSocket echo</title>
+<link rel="stylesheet" href="/style.css">
+</head>
+<body>
+
+<form>
+  <label for="multiline">History:</label><br>
+  <textarea id="multiline" name="multiline" rows="15" cols="50" readonly></textarea><br>
+  <label for="singleline">Send a message:</label><br>
+  <input type="text" id="singleline" placeholder="write a message and press enter" name="singleline"><br>
+  <input type="submit" id="sendButton" value="Send" disabled>
+  <input type="button" id="connectButton" value="Connect" onclick="rebind()">
+</form>
+
+<script>
+rebind();
+
+var form = document.querySelector('form');
+var singleline = document.querySelector('#singleline');
+var multiline = document.querySelector('#multiline');
+var sendButton = document.querySelector('#sendButton');
+var connectButton = document.querySelector('#connectButton');
+
+singleline.focus();
+
+form.addEventListener('submit', function(event){
+  event.preventDefault();
+  ws.send(singleline.value);
+  multiline.value += 'You: ' + singleline.value + '\n';
+  singleline.value = '';
+});
+
+function rebind(){
+  ws = new WebSocket('ws://localhost:8080/echo');
+  
+  ws.onmessage = function(event){
+    multiline.value += 'Server: ' + event.data + '\n';
+  };
+  
+  ws.onopen = function(){
+    sendButton.disabled = false;
+    connectButton.disabled = true;
+  };
+  
+  ws.onclose = function(){
+    sendButton.disabled = true;
+    connectButton.disabled = false;
+  };
+}
+
+</script>
+</body>
+</html>

+ 71 - 0
stest/static/style.css

@@ -0,0 +1,71 @@
+body{
+font-family:Arial, sans-serif;
+background-color:#f2f2f2;
+margin:0;
+padding:20px;
+}
+
+form{
+background-color:#fff;
+padding:20px;
+border-radius:5px;
+box-shadow:0 2px 5px rgba(0, 0, 0, 0.1);
+width:600px;
+margin-left:auto;
+margin-right:auto;
+}
+
+label{
+font-weight:bold;
+}
+
+textarea{
+padding:5px;
+font-size:0.8em;
+width:100%;
+resize:none;
+border-radius:5px;
+margin-bottom:15px;
+margin-top:10px;
+box-sizing:border-box;
+}
+
+input[type="text"]{
+box-sizing:border-box;
+margin-top:10px;
+width:100%;
+padding:10px;
+border:1px solid #ccc;
+border-radius:5px;
+margin-bottom:20px;
+}
+
+input[type="button"]{
+width:150px;
+padding:10px 20px;
+background-color:#50AF4C;
+color:#fff;
+border:none;
+border-radius:3px;
+cursor:pointer;
+}
+
+input[type="button"]:disabled{
+background-color:#ccc;
+cursor:not-allowed;
+}
+
+input[type="submit"]{
+width:150px;
+padding:10px 20px;
+background-color:#4CAF50;
+color:#fff;
+border:none;
+border-radius:5px;
+cursor:pointer;
+}
+
+input[type="submit"]:disabled{
+background-color:#ccc;
+cursor:not-allowed;
+}