简体   繁体   中英

How to use socket.io-client in KotlinJS

I want to use the socket.io-client library from nodejs in my React-Kotlin project.

I tried to load it like this:

@file:JsModule("socket.io-client")
@file:JsNonModule

package modules

@JsName("Manager")
external fun socket(uri: String): dynamic

Then client will connect to the server when I call it with this function val s = socket("http://localhost:8080/") but when I call an emit like this s.emit("testemit", mapOf("test" to false)) it doesn't send it. If I change s.emit to something else that does not exist in socket.io-client, I get an error message that the function does not exist.

It should work with something like this:

val socket = kotlinext.js.require("socket.io-client")("http://localhost:8080/")
socket.emit("testemit", json("test" to false)) { result -> println(result) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM