Giter Club home page Giter Club logo

java-stomp's Introduction

WebSocket 脚手架项目

使用 STOMP over SockJS 实现的 SpringBoot 脚手架项目,可用于快速搭建 WebSocket 工程(支持集群)

使用

  1. 启动项目
  2. 浏览器打开 http://localhost:8025
  3. 登录 (账号: admin 密码: 123456)
  4. 发送任意的文本内容,服务器将回复相同的内容并加上 Echo 前缀

效果展示:

效果展示

当收到消息时,服务端会打印当前在线人数:

当前本系统总在线人数: 1, 当前用户: admin, 该用户的客户端连接数: 1

WebSocket 集群

Spring 是支持 WebSocket 集群的,但是网上很少有资料介绍,官方文档对于相关的配置说明也比较模糊

为此,作者查了许多资料和源码,具体配置可以查看 WebSocketConfig类,里面有非常详细的注释说明

WebSocket 集群必须依赖全功能的消息Broker(full-featured message broker),推荐使用 RabbitMQ

安装 RabbitMQ

具体查看 RabbitMQ安装和集群搭建教程

配置项

websocket:
  broker:
     use-simple-broker: false
     username: test
     password: Passw0rd
     virtualHost: "/stomp"
#    地址可配置一个或多个,当其中一个不可用时,会自动切换以保证高可用
     addresses:
      - 127.0.0.1:61613

Nginx 配置

WebSocket 在连接的时候需要做一次连接升级,而 Nginx 默认是不支持连接升级的,因此如果需要使用 Nginx 做负载均衡的话,要添加以下配置

location /ws{
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   # 指定域名
   proxy_pass   域名;
   proxy_redirect off;
}

当然,如果不添加此配置,SockJS 会为我们自动降级,也能保证可用,但是性能会降低。

参考

java-stomp's People

Contributors

dadiyang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.