feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
This commit is contained in:
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-i", {"long": "--include", "action": "append"})
|
||||
|
||||
var args = parser.parseArgs(["-i", "foo", "-i", "bar", "--include", "baz"])
|
||||
System.print(args["include"].count) // expect: 3
|
||||
System.print(args["include"][0]) // expect: foo
|
||||
System.print(args["include"][1]) // expect: bar
|
||||
System.print(args["include"][2]) // expect: baz
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-v", {"action": "count"})
|
||||
|
||||
var args = parser.parseArgs([])
|
||||
System.print(args["v"]) // expect: 0
|
||||
|
||||
var args2 = parser.parseArgs(["-v"])
|
||||
System.print(args2["v"]) // expect: 1
|
||||
|
||||
var args3 = parser.parseArgs(["-v", "-v", "-v"])
|
||||
System.print(args3["v"]) // expect: 3
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-v", {"long": "--verbose", "action": "storeTrue"})
|
||||
parser.addArgument("-q", {"action": "storeFalse", "dest": "verbose_output"})
|
||||
|
||||
var args = parser.parseArgs([])
|
||||
System.print(args["verbose"]) // expect: false
|
||||
System.print(args["verbose_output"]) // expect: true
|
||||
|
||||
var args2 = parser.parseArgs(["-v"])
|
||||
System.print(args2["verbose"]) // expect: true
|
||||
|
||||
var args3 = parser.parseArgs(["-q"])
|
||||
System.print(args3["verbose_output"]) // expect: false
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-o", {"long": "--output", "default": "out.txt"})
|
||||
parser.addArgument("-n", {"type": "int", "default": 1})
|
||||
|
||||
var args = parser.parseArgs([])
|
||||
System.print(args["output"]) // expect: out.txt
|
||||
System.print(args["n"]) // expect: 1
|
||||
|
||||
var args2 = parser.parseArgs(["-o", "result.txt", "-n", "5"])
|
||||
System.print(args2["output"]) // expect: result.txt
|
||||
System.print(args2["n"]) // expect: 5
|
||||
|
||||
var args3 = parser.parseArgs(["--output", "final.txt"])
|
||||
System.print(args3["output"]) // expect: final.txt
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("filename")
|
||||
parser.addArgument("output", {"required": false, "default": "out.txt"})
|
||||
|
||||
var args = parser.parseArgs(["input.txt"])
|
||||
System.print(args["filename"]) // expect: input.txt
|
||||
System.print(args["output"]) // expect: out.txt
|
||||
|
||||
var args2 = parser.parseArgs(["input.txt", "result.txt"])
|
||||
System.print(args2["filename"]) // expect: input.txt
|
||||
System.print(args2["output"]) // expect: result.txt
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "bytes" for Bytes
|
||||
|
||||
System.print(Bytes.length("Hello")) // expect: 5
|
||||
System.print(Bytes.length("")) // expect: 0
|
||||
|
||||
System.print(Bytes.toList("AB")) // expect: [65, 66]
|
||||
System.print(Bytes.toList("")) // expect: []
|
||||
|
||||
System.print(Bytes.fromList([65, 66, 67])) // expect: ABC
|
||||
System.print(Bytes.fromList([])) // expect:
|
||||
|
||||
System.print(Bytes.concat("Hello", " World")) // expect: Hello World
|
||||
System.print(Bytes.concat("", "Test")) // expect: Test
|
||||
System.print(Bytes.concat("Test", "")) // expect: Test
|
||||
|
||||
System.print(Bytes.slice("Hello", 0, 2)) // expect: He
|
||||
System.print(Bytes.slice("Hello", 2, 5)) // expect: llo
|
||||
System.print(Bytes.slice("Hello", 0, 100)) // expect: Hello
|
||||
System.print(Bytes.length(Bytes.slice("Hello", 10, 20))) // expect: 0
|
||||
|
||||
var mask = Bytes.fromList([0xFF, 0xFF, 0xFF, 0xFF])
|
||||
var data = Bytes.fromList([0, 1, 2, 3])
|
||||
var xored = Bytes.xorMask(data, mask)
|
||||
System.print(Bytes.toList(xored)) // expect: [255, 254, 253, 252]
|
||||
|
||||
var mask2 = Bytes.fromList([0x12, 0x34])
|
||||
var data2 = Bytes.fromList([0, 0, 0, 0])
|
||||
var xored2 = Bytes.xorMask(data2, mask2)
|
||||
System.print(Bytes.toList(xored2)) // expect: [18, 52, 18, 52]
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "dataset" for Dataset
|
||||
|
||||
var ds = Dataset.memory()
|
||||
|
||||
var users = ds["users"]
|
||||
users.insert({"name": "Alice"})
|
||||
|
||||
var cols1 = users.columns
|
||||
System.print(cols1.containsKey("uid")) // expect: true
|
||||
System.print(cols1.containsKey("name")) // expect: true
|
||||
System.print(cols1.containsKey("email")) // expect: false
|
||||
|
||||
users.insert({"name": "Bob", "email": "bob@example.com"})
|
||||
|
||||
var cols2 = users.columns
|
||||
System.print(cols2.containsKey("email")) // expect: true
|
||||
|
||||
ds.close()
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "dataset" for Dataset
|
||||
|
||||
var ds = Dataset.memory()
|
||||
var users = ds["users"]
|
||||
|
||||
var user = users.insert({"name": "Alice", "age": 30})
|
||||
System.print(user["name"]) // expect: Alice
|
||||
System.print(user["age"]) // expect: 30
|
||||
System.print(user["uid"] != null) // expect: true
|
||||
System.print(user["created_at"] != null) // expect: true
|
||||
|
||||
var user2 = users.insert({"name": "Bob", "age": 25, "active": true})
|
||||
System.print(user2["name"]) // expect: Bob
|
||||
System.print(user2["active"]) // expect: true
|
||||
|
||||
System.print(users.count()) // expect: 2
|
||||
|
||||
ds.close()
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "dataset" for Dataset
|
||||
|
||||
var ds = Dataset.memory()
|
||||
var users = ds["users"]
|
||||
|
||||
users.insert({"name": "Alice", "age": 30})
|
||||
users.insert({"name": "Bob", "age": 25})
|
||||
users.insert({"name": "Charlie", "age": 35})
|
||||
|
||||
var all = users.all()
|
||||
System.print(all.count) // expect: 3
|
||||
|
||||
var found = users.find({"age__gt": 28})
|
||||
System.print(found.count) // expect: 2
|
||||
|
||||
var alice = users.findOne({"name": "Alice"})
|
||||
System.print(alice["age"]) // expect: 30
|
||||
|
||||
var young = users.find({"age__lt": 30})
|
||||
System.print(young.count) // expect: 1
|
||||
System.print(young[0]["name"]) // expect: Bob
|
||||
|
||||
ds.close()
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "dataset" for Dataset
|
||||
|
||||
var ds = Dataset.memory()
|
||||
var users = ds["users"]
|
||||
|
||||
var user = users.insert({"name": "Alice", "age": 30})
|
||||
var uid = user["uid"]
|
||||
|
||||
users.update({"uid": uid, "age": 31})
|
||||
var updated = users.findOne({"uid": uid})
|
||||
System.print(updated["age"]) // expect: 31
|
||||
|
||||
System.print(users.count()) // expect: 1
|
||||
System.print(users.delete(uid)) // expect: true
|
||||
System.print(users.count()) // expect: 0
|
||||
|
||||
ds.close()
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Html
|
||||
|
||||
var encoded = Html.encodeParams({"name": "John Doe", "age": 30})
|
||||
System.print(encoded.contains("name=John+Doe")) // expect: true
|
||||
System.print(encoded.contains("age=30")) // expect: true
|
||||
System.print(encoded.contains("&")) // expect: true
|
||||
|
||||
var decoded = Html.decodeParams("name=John+Doe&age=30")
|
||||
System.print(decoded["name"]) // expect: John Doe
|
||||
System.print(decoded["age"]) // expect: 30
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Html
|
||||
|
||||
System.print(Html.quote("<script>")) // expect: <script>
|
||||
System.print(Html.quote("a & b")) // expect: a & b
|
||||
System.print(Html.quote("\"quoted\"")) // expect: "quoted"
|
||||
System.print(Html.quote("it's")) // expect: it's
|
||||
System.print(Html.quote("normal text")) // expect: normal text
|
||||
|
||||
System.print(Html.unquote("<script>")) // expect: <script>
|
||||
System.print(Html.unquote("a & b")) // expect: a & b
|
||||
System.print(Html.unquote(""quoted"")) // expect: "quoted"
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Html
|
||||
|
||||
System.print(Html.slugify("Hello World")) // expect: hello-world
|
||||
System.print(Html.slugify("This is a TEST")) // expect: this-is-a-test
|
||||
System.print(Html.slugify("foo---bar")) // expect: foo-bar
|
||||
System.print(Html.slugify(" spaces ")) // expect: spaces
|
||||
System.print(Html.slugify("test123")) // expect: test123
|
||||
System.print(Html.slugify("UPPERCASE")) // expect: uppercase
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Html
|
||||
|
||||
System.print(Html.urldecode("hello+world")) // expect: hello world
|
||||
|
||||
var encoded1 = "foo" + "\%3D" + "bar"
|
||||
System.print(Html.urldecode(encoded1)) // expect: foo=bar
|
||||
|
||||
var encoded2 = "a" + "\%26" + "b"
|
||||
System.print(Html.urldecode(encoded2)) // expect: a&b
|
||||
|
||||
System.print(Html.urldecode("test")) // expect: test
|
||||
System.print(Html.urldecode("")) // expect:
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Html
|
||||
|
||||
System.print(Html.urlencode("hello world")) // expect: hello+world
|
||||
System.print(Html.urlencode("foo=bar")) // expect: foo%3Dbar
|
||||
System.print(Html.urlencode("a&b")) // expect: a%26b
|
||||
System.print(Html.urlencode("test")) // expect: test
|
||||
System.print(Html.urlencode("")) // expect:
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "markdown" for Markdown
|
||||
|
||||
var code = Markdown.toHtml("```\ncode here\n```")
|
||||
System.print(code.contains("<pre><code>")) // expect: true
|
||||
System.print(code.contains("code here")) // expect: true
|
||||
System.print(code.contains("</code></pre>")) // expect: true
|
||||
|
||||
var quote = Markdown.toHtml("> quoted text")
|
||||
System.print(quote.contains("<blockquote>")) // expect: true
|
||||
System.print(quote.contains("quoted text")) // expect: true
|
||||
System.print(quote.contains("</blockquote>")) // expect: true
|
||||
|
||||
var hr = Markdown.toHtml("---")
|
||||
System.print(hr.contains("<hr>")) // expect: true
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "markdown" for Markdown
|
||||
|
||||
System.print(Markdown.toHtml("**bold**")) // expect: <p><strong>bold</strong></p>
|
||||
System.print(Markdown.toHtml("*italic*")) // expect: <p><em>italic</em></p>
|
||||
System.print(Markdown.toHtml("`code`")) // expect: <p><code>code</code></p>
|
||||
System.print(Markdown.toHtml("~~deleted~~")) // expect: <p><del>deleted</del></p>
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "markdown" for Markdown
|
||||
|
||||
System.print(Markdown.toHtml("# Heading 1")) // expect: <h1>Heading 1</h1>
|
||||
System.print(Markdown.toHtml("## Heading 2")) // expect: <h2>Heading 2</h2>
|
||||
System.print(Markdown.toHtml("### Heading 3")) // expect: <h3>Heading 3</h3>
|
||||
System.print(Markdown.toHtml("###### Heading 6")) // expect: <h6>Heading 6</h6>
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "markdown" for Markdown
|
||||
|
||||
var link = Markdown.toHtml("[Google](https://google.com)")
|
||||
System.print(link.contains("<a href=\"https://google.com\">Google</a>")) // expect: true
|
||||
|
||||
var img = Markdown.toHtml("")
|
||||
System.print(img.contains("<img src=\"image.png\" alt=\"Alt text\">")) // expect: true
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "markdown" for Markdown
|
||||
|
||||
var ul = Markdown.toHtml("- item 1\n- item 2")
|
||||
System.print(ul.contains("<ul>")) // expect: true
|
||||
System.print(ul.contains("<li>item 1</li>")) // expect: true
|
||||
System.print(ul.contains("<li>item 2</li>")) // expect: true
|
||||
System.print(ul.contains("</ul>")) // expect: true
|
||||
|
||||
var ol = Markdown.toHtml("1. first\n2. second")
|
||||
System.print(ol.contains("<ol>")) // expect: true
|
||||
System.print(ol.contains("<li>first</li>")) // expect: true
|
||||
System.print(ol.contains("</ol>")) // expect: true
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "uuid" for Uuid
|
||||
|
||||
var u1 = Uuid.v4()
|
||||
System.print(u1.count) // expect: 36
|
||||
|
||||
System.print(u1[8] == "-") // expect: true
|
||||
System.print(u1[13] == "-") // expect: true
|
||||
System.print(u1[18] == "-") // expect: true
|
||||
System.print(u1[23] == "-") // expect: true
|
||||
|
||||
System.print(u1[14]) // expect: 4
|
||||
|
||||
var variant = u1[19]
|
||||
System.print(variant == "8" || variant == "9" || variant == "a" || variant == "b") // expect: true
|
||||
|
||||
System.print(Uuid.isValid(u1)) // expect: true
|
||||
System.print(Uuid.isV4(u1)) // expect: true
|
||||
|
||||
System.print(Uuid.isValid("550e8400-e29b-41d4-a716-446655440000")) // expect: true
|
||||
System.print(Uuid.isValid("invalid-uuid")) // expect: false
|
||||
System.print(Uuid.isValid(123)) // expect: false
|
||||
System.print(Uuid.isValid("550e8400-e29b-41d4-a716-44665544000X")) // expect: false
|
||||
|
||||
var u2 = Uuid.v4()
|
||||
System.print(u1 != u2) // expect: true
|
||||
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "wdantic" for Schema, Field, ValidationResult
|
||||
|
||||
var userSchema = Schema.new({
|
||||
"name": Field.string({"minLength": 1}),
|
||||
"age": Field.integer({"min": 0, "max": 150}),
|
||||
"email": Field.email()
|
||||
})
|
||||
|
||||
var validData = {"name": "John", "age": 30, "email": "john@example.com"}
|
||||
var result = userSchema.validate(validData)
|
||||
System.print(result.isValid) // expect: true
|
||||
System.print(result.data["name"]) // expect: John
|
||||
System.print(result.data["age"]) // expect: 30
|
||||
|
||||
var invalidData = {"name": "", "age": -5, "email": "invalid"}
|
||||
var result2 = userSchema.validate(invalidData)
|
||||
System.print(result2.isValid) // expect: false
|
||||
System.print(result2.errors.count > 0) // expect: true
|
||||
|
||||
var optionalSchema = Schema.new({
|
||||
"name": Field.string(),
|
||||
"nickname": Field.optional(Field.string())
|
||||
})
|
||||
|
||||
var data3 = {"name": "Alice"}
|
||||
var result3 = optionalSchema.validate(data3)
|
||||
System.print(result3.isValid) // expect: true
|
||||
|
||||
var listSchema = Schema.new({
|
||||
"tags": Field.list(Field.string())
|
||||
})
|
||||
var data4 = {"tags": ["a", "b", "c"]}
|
||||
var result4 = listSchema.validate(data4)
|
||||
System.print(result4.isValid) // expect: true
|
||||
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "wdantic" for Validator
|
||||
|
||||
System.print(Validator.email("test@example.com")) // expect: true
|
||||
System.print(Validator.email("invalid")) // expect: false
|
||||
System.print(Validator.email("test@domain")) // expect: false
|
||||
|
||||
System.print(Validator.domain("example.com")) // expect: true
|
||||
System.print(Validator.domain("sub.example.com")) // expect: true
|
||||
System.print(Validator.domain("-invalid.com")) // expect: false
|
||||
|
||||
System.print(Validator.safeStr("Hello World")) // expect: true
|
||||
System.print(Validator.safeStr("Line1\nLine2")) // expect: false
|
||||
|
||||
System.print(Validator.url("https://example.com")) // expect: true
|
||||
System.print(Validator.url("http://localhost:8080/path")) // expect: true
|
||||
System.print(Validator.url("ftp://invalid")) // expect: false
|
||||
|
||||
System.print(Validator.uuid("550e8400-e29b-41d4-a716-446655440000")) // expect: true
|
||||
System.print(Validator.uuid("invalid")) // expect: false
|
||||
|
||||
System.print(Validator.minLength("hello", 3)) // expect: true
|
||||
System.print(Validator.minLength("hi", 3)) // expect: false
|
||||
|
||||
System.print(Validator.maxLength("hi", 5)) // expect: true
|
||||
System.print(Validator.maxLength("hello world", 5)) // expect: false
|
||||
|
||||
System.print(Validator.range(5, 1, 10)) // expect: true
|
||||
System.print(Validator.range(15, 1, 10)) // expect: false
|
||||
|
||||
System.print(Validator.positive(5)) // expect: true
|
||||
System.print(Validator.positive(-5)) // expect: false
|
||||
|
||||
System.print(Validator.integer(5)) // expect: true
|
||||
System.print(Validator.integer(5.5)) // expect: false
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "web" for Response
|
||||
|
||||
var r1 = Response.text("Hello")
|
||||
System.print(r1.body) // expect: Hello
|
||||
System.print(r1.headers["Content-Type"].contains("text/plain")) // expect: true
|
||||
|
||||
var r2 = Response.html("<h1>Hi</h1>")
|
||||
System.print(r2.body) // expect: <h1>Hi</h1>
|
||||
System.print(r2.headers["Content-Type"].contains("text/html")) // expect: true
|
||||
|
||||
var r3 = Response.json({"name": "test"})
|
||||
System.print(r3.body.contains("name")) // expect: true
|
||||
System.print(r3.headers["Content-Type"].contains("application/json")) // expect: true
|
||||
|
||||
var r4 = Response.redirect("/login")
|
||||
System.print(r4.status) // expect: 302
|
||||
System.print(r4.headers["Location"]) // expect: /login
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "web" for Router
|
||||
|
||||
var router = Router.new()
|
||||
router.get("/", Fn.new { |r| "home" })
|
||||
router.get("/users/:id", Fn.new { |r| "user" })
|
||||
router.post("/api/data", Fn.new { |r| "data" })
|
||||
|
||||
var m1 = router.match("GET", "/")
|
||||
System.print(m1 != null) // expect: true
|
||||
|
||||
var m2 = router.match("GET", "/users/123")
|
||||
System.print(m2 != null) // expect: true
|
||||
System.print(m2["params"]["id"]) // expect: 123
|
||||
|
||||
var m3 = router.match("POST", "/api/data")
|
||||
System.print(m3 != null) // expect: true
|
||||
|
||||
var m4 = router.match("GET", "/notfound")
|
||||
System.print(m4 == null) // expect: true
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "web" for SessionStore
|
||||
|
||||
var store = SessionStore.new()
|
||||
var session = store.create()
|
||||
System.print(session.id != null) // expect: true
|
||||
System.print(session.id.count) // expect: 36
|
||||
|
||||
session["user"] = "alice"
|
||||
System.print(session["user"]) // expect: alice
|
||||
|
||||
store.save(session)
|
||||
var retrieved = store.get(session.id)
|
||||
System.print(retrieved != null) // expect: true
|
||||
System.print(retrieved["user"]) // expect: alice
|
||||
|
||||
store.destroy(session.id)
|
||||
System.print(store.get(session.id) == null) // expect: true
|
||||
Vendored
+69
@@ -0,0 +1,69 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "websocket" for WebSocket, WebSocketServer
|
||||
import "scheduler" for Scheduler
|
||||
import "timer" for Timer
|
||||
|
||||
var port = 19877
|
||||
var payloadSize = 3 * 1024 * 1024
|
||||
var result = "pending"
|
||||
|
||||
Scheduler.add {
|
||||
var server = WebSocketServer.bind("127.0.0.1", port)
|
||||
var ws = server.accept()
|
||||
|
||||
if (ws != null) {
|
||||
var msg = ws.receive()
|
||||
if (msg != null && msg.isBinary) {
|
||||
ws.sendBinary(msg.bytes)
|
||||
}
|
||||
ws.close()
|
||||
}
|
||||
server.close()
|
||||
}
|
||||
|
||||
Timer.sleep(50)
|
||||
|
||||
Scheduler.add {
|
||||
var ws = WebSocket.connect("ws://127.0.0.1:%(port)")
|
||||
|
||||
var payload = []
|
||||
var i = 0
|
||||
while (i < payloadSize) {
|
||||
payload.add(i % 256)
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
ws.sendBinary(payload)
|
||||
|
||||
var response = ws.receive()
|
||||
|
||||
if (response == null) {
|
||||
result = "error: no response"
|
||||
} else if (!response.isBinary) {
|
||||
result = "error: not binary"
|
||||
} else if (response.bytes.count != payloadSize) {
|
||||
result = "error: size mismatch %(response.bytes.count) != %(payloadSize)"
|
||||
} else {
|
||||
var mismatch = false
|
||||
i = 0
|
||||
while (i < payloadSize) {
|
||||
if (response.bytes[i] != (i % 256)) {
|
||||
mismatch = true
|
||||
break
|
||||
}
|
||||
i = i + 1
|
||||
}
|
||||
if (mismatch) {
|
||||
result = "error: data mismatch at %(i)"
|
||||
} else {
|
||||
result = "ok"
|
||||
}
|
||||
}
|
||||
|
||||
ws.close()
|
||||
}
|
||||
|
||||
Timer.sleep(30000)
|
||||
|
||||
System.print(result) // expect: ok
|
||||
Reference in New Issue
Block a user