websocket路径配置备忘--以spring中配置为例

/*
 * Name: WebSocketConfig.java
 * Title:
 * Description:
 * @copyright 2004-2018 Sichuan Tianyi Network Service Co.,Ltd All Rights Reserved。
 * @version v1.0.0 2016-4-25 14:19:09
 */
package com.tievd.cloud.vbp.message.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;

/**
 *
 * @author sitianqiang@tieserv.com
 */
@Configuration
@EnableWebSocketMessageBroker
@Order(Ordered.HIGHEST_PRECEDENCE + 99)
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    private Log LOG = LogFactory.getLog(WebSocketConfig.class);

    /**
     * 定义服务器端接改地址
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //服务端点接收客户端的连接
        registry.addEndpoint("/notify").setAllowedOrigins("*").withSockJS().setClientLibraryUrl("../js/1.1.4/sockjs.min.js");
    }

    /**
     * 定义服务器端发送地址
     *
     * @param config
     */
    @Override
    **public void configureMessageBroker(MessageBrokerRegistry config) {
        //客户端给服务端发消息的地址的前缀
        config.setApplicationDestinationPrefixes("/app");
        //客户端接收服务端消息的地址的前缀信息
        config.enableSimpleBroker("/topic", "/queue");
    }**
}

在以上服务端配置的情况下,js也有相应的写法:
连接websocket:很明显对应者registerStompEndpoints方法里的配置notify

var socket = new SockJS("http://master:8989/msg/notify/?access_token=" + token);  

订阅:对应于enableSimpleBroker方法

                stompClient.subscribe('/user/queue/face_compare', function(greeting){  

客户端向服务端发送消息:对应与setApplicationDestinationPrefixes方法

     stompClient.send("/app/hello", {atytopic:"greetings"}, JSON.stringify({ 'name': name }));  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值