Giter VIP home page Giter VIP logo

Comments (2)

meqtMac avatar meqtMac commented on July 30, 2024 3

@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.

MihaelIsaev avatar MihaelIsaev commented on July 30, 2024 1

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.