该项目的目标是让 JVM(Kotlin)服务器和 Python 客户端通过 websocket 相互通信。服务器必须向客户端发送更新,客户端将处理这些更新。
服务器是一个 Spring Boot 应用程序,运行 Spring Boot websocket 服务器。客户端现在只是一个 Python 脚本,运行 websocket-client 来连接到服务器。
什么工作:
什么不起作用:
我已经通过连接到 websocket.org (ws://echo.websocket.org) 的回显服务器尝试了客户端的接收部分,并且客户端从服务器接收回显消息。所以在我看来,问题不在客户端。
Kotlin 服务器: 用于创建 websocket 服务器的代码:
package nl.sajansen.screenchangenotifierserver
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.simp.SimpMessagingTemplate
import org.springframework.messaging.simp.config.MessageBrokerRegistry
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Controller
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
import org.springframework.web.socket.config.annotation.StompEndpointRegistry
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer
import java.time.Instant
import java.time.format.DateTimeFormatter
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig : WebSocketMessageBrokerConfigurer {
override fun registerStompEndpoints(registry: StompEndpointRegistry) {
registry.addEndpoint("/ws").setAllowedOrigins("*")
}
override …Run Code Online (Sandbox Code Playgroud)