icerockdev / moko-socket-io

MOKO SocketIo by IceRock is Socket.IO implementation Kotlin Multiplatform library
https://moko.icerock.dev/
Apache License 2.0
116 stars 19 forks source link

websockets support #3

Closed ln-12 closed 3 years ago

ln-12 commented 3 years ago

I just discovered your library and was wondering, if I can also use it for eventless communication via websockets. Let's say for testing I want to send a string to wss://echo.websocket.org and get it back without any special event. Is that possible?

Alex009 commented 3 years ago

hi! sockets by default sent ping-pong events to check connection and you can subscribe to this events: https://github.com/icerockdev/moko-socket-io/blob/master/socket-io/src/commonMain/kotlin/dev/icerock/moko/socket/SocketEvent.kt#L15

ln-12 commented 3 years ago

I don't know if you understand my question right. I can't handle any events as the library fails to connect to the server. I am using your example here and replaced the code with:

/*
 * Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
 */

package com.icerockdev.library

import dev.icerock.moko.socket.Socket
import dev.icerock.moko.socket.SocketEvent
import dev.icerock.moko.socket.SocketOptions

class Testing {
    val socket = Socket(
        endpoint = "https://echo.websocket.org",
        config = SocketOptions(
            queryParams = null,
            transport = SocketOptions.Transport.WEBSOCKET
        )
    ) {
        on(SocketEvent.Connect) {
            println("connect")
        }

        on(SocketEvent.Connecting) {
            println("connecting")
        }

        on(SocketEvent.Disconnect) {
            println("disconnect")
        }

        on(SocketEvent.Error) {
            println("error $it")
        }

        on(SocketEvent.Reconnect) {
            println("reconnect")
        }

        on(SocketEvent.ReconnectAttempt) {
            println("reconnect attempt $it")
        }

        on(SocketEvent.Ping) {
            println("ping")
        }

        on(SocketEvent.Pong) {
            println("pong")
        }
    }

    fun login() {
        socket.emit("ping", "Hello World")
    }
}

But I just get the error message:

error io.socket.engineio.client.EngineIOException: websocket error
I/System.out: reconnect attempt 1
I/System.out: error io.socket.engineio.client.EngineIOException: websocket error
I/System.out: error io.socket.client.SocketIOException: Connection error
I/System.out: reconnect attempt 2

So how do I have to change the code above to connect to the echo server?

Alex009 commented 3 years ago

socket.io is not clear websocket. as socket.io declare in docs - https://socket.io/docs/v3/index.html#What-Socket-IO-is-not so i think we just should add another engine with clean websockets support

Alex009 commented 3 years ago

more info in #5 so i think we should close this issue

ln-12 commented 3 years ago

Yeah, I also noticed that. I am still hoping, that Ktor implements websocket support for iOS any time soon because other platforms are already supported.