Android 设备上的 ktor websocket 安装错误
ktor websocket install error on Android device
我正在尝试 运行 在带有 ktor websocket 的 Android 设备上进行简单的聊天示例,但效果不佳。
我在 MainActivity
中安装 websocket 服务器时出错
这里是 build.gradle ktor websocket
//this is for project
ext.ktor_version = '1.2.5'
maven { url "https://dl.bintray.com/kotlin/ktor" }
//this is for app
packagingOptions {
exclude 'META-INF/*'
}
implementation "io.ktor:ktor-websockets:$ktor_version"
这是 MainActivity 的代码
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.cio.websocket.*
import io.ktor.http.cio.websocket.CloseReason
import io.ktor.http.cio.websocket.Frame
import io.ktor.http.content.*
import io.ktor.routing.*
import io.ktor.sessions.*
import io.ktor.util.*
import io.ktor.websocket.*
import kotlinx.coroutines.channels.*
import java.time.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
install(WebSockets) {
pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default
timeout = Duration.ofSeconds(15)
maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length.
masking = false
}
}
}
当我在 Android Studio 上构建此代码时,出现错误
public fun <P : Pipeline<*, ApplicationCall>, B : Any, F : Any> ???.install(feature: ApplicationFeature<???, WebSockets.WebSocketOptions, WebSockets>, configure: WebSockets.WebSocketOptions.() -> Unit = ...): WebSockets defined in io.ktor.application
能否告知您当前的 Kotlin 版本和正在使用的 Gradle 版本?如果 Kotlin 版本低于 1.4,那么我建议尝试将 -jvm
或 -android
添加到 build.gradle.
中的 ktor 依赖项名称
Websockets is one of the server features that you can plug into an application, so the context for the install
call should be a Ktor application, not an Android Activity
. I recommend you to look at Hello world 示例。这是您修改后的示例代码:
import io.ktor.application.*
import io.ktor.http.cio.websocket.*
import io.ktor.server.engine.*
import io.ktor.websocket.*
import java.time.Duration
import io.ktor.routing.*
import io.ktor.server.netty.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
embeddedServer(Netty, 4444) {
install(WebSockets) {
pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default
timeout = Duration.ofSeconds(15)
maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length.
masking = false
}
routing {
webSocket("/") {
// ...
}
}
}.start()
}
}
您需要添加以下依赖项才能使此示例正常工作:
implementation "io.ktor:ktor-server-core:$ktor_version"
implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "ch.qos.logback:logback-classic:1.2.3" // for logging
我正在尝试 运行 在带有 ktor websocket 的 Android 设备上进行简单的聊天示例,但效果不佳。 我在 MainActivity
中安装 websocket 服务器时出错这里是 build.gradle ktor websocket
//this is for project
ext.ktor_version = '1.2.5'
maven { url "https://dl.bintray.com/kotlin/ktor" }
//this is for app
packagingOptions {
exclude 'META-INF/*'
}
implementation "io.ktor:ktor-websockets:$ktor_version"
这是 MainActivity 的代码
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.cio.websocket.*
import io.ktor.http.cio.websocket.CloseReason
import io.ktor.http.cio.websocket.Frame
import io.ktor.http.content.*
import io.ktor.routing.*
import io.ktor.sessions.*
import io.ktor.util.*
import io.ktor.websocket.*
import kotlinx.coroutines.channels.*
import java.time.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
install(WebSockets) {
pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default
timeout = Duration.ofSeconds(15)
maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length.
masking = false
}
}
}
当我在 Android Studio 上构建此代码时,出现错误
public fun <P : Pipeline<*, ApplicationCall>, B : Any, F : Any> ???.install(feature: ApplicationFeature<???, WebSockets.WebSocketOptions, WebSockets>, configure: WebSockets.WebSocketOptions.() -> Unit = ...): WebSockets defined in io.ktor.application
能否告知您当前的 Kotlin 版本和正在使用的 Gradle 版本?如果 Kotlin 版本低于 1.4,那么我建议尝试将 -jvm
或 -android
添加到 build.gradle.
Websockets is one of the server features that you can plug into an application, so the context for the install
call should be a Ktor application, not an Android Activity
. I recommend you to look at Hello world 示例。这是您修改后的示例代码:
import io.ktor.application.*
import io.ktor.http.cio.websocket.*
import io.ktor.server.engine.*
import io.ktor.websocket.*
import java.time.Duration
import io.ktor.routing.*
import io.ktor.server.netty.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
embeddedServer(Netty, 4444) {
install(WebSockets) {
pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default
timeout = Duration.ofSeconds(15)
maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length.
masking = false
}
routing {
webSocket("/") {
// ...
}
}
}.start()
}
}
您需要添加以下依赖项才能使此示例正常工作:
implementation "io.ktor:ktor-server-core:$ktor_version"
implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "ch.qos.logback:logback-classic:1.2.3" // for logging