spring boot 使用微博socket

websocket

https://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js

https://cdn.jsdelivr.net/sockjs/1/sockjs.min.js

http://blog.csdn.net/PandaWang1989/article/details/54863489?locationNum=5&fps=1

https://www.cnblogs.com/bianzy/p/5822426.html

http://blog.csdn.net/u012373815/article/details/54380476

https://segmentfault.com/a/1190000011908831

===========

http://blog.csdn.net/javaexploreroooo/article/details/52432111

http://blog.csdn.net/soslinken/article/details/53021510

http://blog.csdn.net/jie873440996/article/details/70257558

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="utf-8"/>
    <meta name="renderer" content="webkit|ie-comp|ie-stand"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <title>Key to Happiness</title>
    <link rel="stylesheet" href="/static/css/common.css"/>
</head>
<body>
<header class="navbar-wrapper">
    测试 webSocket
    <button οnclick="connect()">连接</button>
    <button οnclick="disconnect()">断开</button>

    <button οnclick="sendName()">发送信息</button>
</header>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/sockjs.min.js"></script>
<script type="text/javascript" src="/static/js/stomp.min.js"></script>
<script type="text/javascript">
    var stompClient = null;

    function setConnected(connected){
        /*document.getElementById('connect').disabled = connected;
        document.getElementById('disconnect').disabled = !connected;
        document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';*/
        $("#response").html();
    }

    function connect(){
        var socket = new SockJS('/endpointAric'); //连接SockJS的endpoint名称为"endpointWisely"
        stompClient = Stomp.over(socket);//使用STMOP子协议的WebSocket客户端
        stompClient.connect({},function(frame){//连接WebSocket服务端
            setConnected(true);
            console.log('Connected:' + frame);
            //通过stompClient.subscribe订阅/topic/getResponse 目标(destination)发送的消息,这个是在控制器的@SentTo中定义的
            stompClient.subscribe('/topic/getResponse',function(response){
                console.log('-----------');
                console.log(response);
                console.log('-----------');
            });
        });
    }

    function disconnect(){
        if(stompClient != null) {
            stompClient.disconnect();
        }
        setConnected(false);
        console.log("Disconnected");
    }

    function sendName(){
        var name = "测试数据"//$("#name").val();
        //通过stompClient.send向/welcome 目标(destination)发送消息,这个是在控制器的@MessageMapping中定义的
        stompClient.send("/welcome",{},JSON.stringify({'name':name}));
    }

    function showResponse(message){
        var response = $("#response");
        response.html(message);
    }
</script>
</body>
</html>

转载于:https://my.oschina.net/u/1538767/blog/1577331

要在Spring Boot中集成Socket.io,可以使用 Spring Boot Starter for Socket.IO。 首先,需要在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId>netty-socketio</artifactId> <version>1.7.16</version> </dependency> <dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId>socketio-spring-boot-starter</artifactId> <version>1.7.16</version> </dependency> ``` 接下来,在Spring Boot应用程序中创建一个WebSocket配置类,如下所示: ``` @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS(); } } ``` 在这个配置类中,我们启用了一个简单的消息代理,并注册了一个用于WebSocket连接的端点。还需要为Socket.io设置一个端口,如下所示: ``` @Configuration @EnableConfigurationProperties(SocketConfigProperties.class) public class SocketConfig { @Autowired private SocketConfigProperties properties; @Bean public SocketIOServer socketIOServer() { Configuration config = new Configuration(); config.setHostname(properties.getHost()); config.setPort(properties.getPort()); SocketIOServer server = new SocketIOServer(config); return server; } } ``` 最后,在应用程序的主类中使用 @EnableSocketIOServer 注解启用Socket.IO服务器,如下所示: ``` @SpringBootApplication @EnableSocketIOServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 现在,您已经在Spring Boot应用程序中成功集成了Socket.IO。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值