Comments (2)
@Jomy10 you could take a look at https://github.com/swiftwasm/WebAPIKit, WebSocket package. I haven't used WebSocket, but WebAPIKit include a great amount of MDB Web API. I have been working with CanvasRenderingContext2D and here is an example
canvas.element.addEventListener(type: "mousemove") { event in
if (!running) {
let mouseEvent = MouseEvent(unsafelyWrapping: event.jsObject)
clear(on: context)
ball.x = Double(mouseEvent.clientX);
ball.y = Double(mouseEvent.clientY);
ball.draw(on: context)
}
}
canvas.element.addEventListener(type: "click") { _ in
if (!running) {
ref = globalThis.requestAnimationFrame { _ in
draw(ball: ball, on: context)
}
running = true
}
}
canvas.element.addEventListener(type: "mouseout") { event in
guard let ref else { return }
globalThis.cancelAnimationFrame(handle: ref)
running = false
}
ball.draw(on: canvas.context)
you can also look for MDN's api document and call functions with dynmaic lookup, anyway without IDE autocomplete. By, WebAPIKit, you can call functions with autocomplete just like native swift program with little friction.
from javascriptkit.
@Jomy10 you could take a look at fully working implementation here swifweb/WebSocketAPI
Example usage
import WebSocket
let webSocket = WebSocket("wss://echo.websocket.org").onOpen {
print("ws connected")
// send as simple string
webSocket.send("Hello from SwifWeb")
// send as Blob
webSocket.send(Blob("Hello from SwifWeb"))
}.onClose { (closeEvent: CloseEvent) in
print("ws disconnected code: \(closeEvent.code) reason: \(closeEvent.reason)")
}.onError {
print("ws error")
}.onMessage { message in
print("ws message: \(message)")
switch message.data {
case .arrayBuffer(let arrayBuffer): break
case .blob(let blob): break
case .text(let text): break
case .unknown(let jsValue): break
}
}
from javascriptkit.
Related Issues (20)
- Calling `sleep()` causes `TypeError: can't convert null to BigInt`
- Enchancement: Add a link to the docs HOT 4
- Export function returning a JSValue HOT 2
- Arguments in JSClosure HOT 4
- JSPromise(resolver:) usage HOT 3
- Asynchronous calls in JSClosure HOT 5
- Making JSObject hashable HOT 5
- Compile error on macOS 12.2.1 HOT 6
- UserAgent support? HOT 1
- Generate JavaScriptKit `.js` runtime with SwiftPM build tool HOT 6
- `JSNumber` type to support `long long` type in Web API IDL
- PUT request using Fetch HOT 2
- Occasional JSClosure has been already released by Swift side HOT 4
- Compiled module was created by a different version of the compiler; rebuild 'JavaScriptKit' and try again HOT 2
- Cannot perform DataView.prototype.setBigUint64 on a detached ArrayBuffer HOT 2
- JSClosure will be overriden from other closure HOT 1
- Are JSValue bitmasks possible? HOT 2
- `JSClosure.async` doesn't work at all
- Init `JSUInt8ClampedArray` from `[UInt8]` failed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascriptkit.