spring-boot-starter-websocket 客户端 服务端 例子

1. 添加依赖

首先,在pom.xml中添加spring-boot-starter-websocket依赖,正如你已经指出的:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

2. 创建WebSocket配置

创建一个配置类来启用和配置WebSocket。

java 复制代码
import org.springframework.context.annotation.Configuration
import org.springframework.web.socket.config.annotation.EnableWebSocket
import org.springframework.web.socket.config.annotation.WebSocketConfigurer
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry


@Configuration
@EnableWebSocket
open class WebSocketConfig : WebSocketConfigurer {
    override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) {
        registry.addHandler(EchoWebSocketHandler(), "/echo")
    }
}

3. 创建WebSocket处理器

创建一个WebSocketHandler来处理消息。

java 复制代码
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class EchoWebSocketHandler extends TextWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        // Echo the received message back
        session.sendMessage(message);
    }
}

4. 启动Spring Boot应用程序

创建Application.java来启动应用程序。

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

5. 创建WebSocket客户端

这里是一个简单的WebSocket客户端示例,使用标准Java WebSocket API。

java 复制代码
import jakarta.websocket.ClientEndpoint
import jakarta.websocket.ContainerProvider
import jakarta.websocket.OnMessage
import jakarta.websocket.Session
import java.net.URI


@ClientEndpoint
class EchoClient {
    @OnMessage
    fun onMessage(message: String, session: Session?) {
        println("Received from server: $message")
    }

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            try {
                val container = ContainerProvider.getWebSocketContainer()
                val uri = "ws://localhost:8080/echo"
                val session: Session = container.connectToServer(EchoClient::class.java, URI.create(uri))
                session.getBasicRemote().sendText("Hello, echo!")
                Thread.sleep(1000) 
                session.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}

启动并测试

启动Spring Boot应用程序,然后运行客户端。客户端会发送一条消息到服务器,服务器应该会把相同的消息回传给客户端。

相关推荐
m0_7482386311 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
一只小姜丝3321 小时前
解决各大浏览器中http地址无权限调用麦克风摄像头问题
网络·vue.js·网络协议·http
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
Hacker_xingchen8 小时前
网络安全扫描--基础篇
网络·安全·web安全
hgdlip9 小时前
如果更换ip地址会怎么样?网络ip地址怎么更换
网络·网络协议·tcp/ip
心随_风动9 小时前
网络七层模型—OSI参考模型详解
网络
桃酥40310 小时前
1.介绍一下TCP/IP模型和OSI模型的区别【中高频】
网络
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle
Mr_wilson_liu10 小时前
ssh被暴力访问了,怎么拦截ip
网络·tcp/ip·ssh