Lv.的博客

ActiveMQ、Stomp、SockJS入门级应用

 

使用ActiveMQ、Stomp、SockJS实现实时在线聊天

 

  ActiveMQ : 强大的开源即时通讯和集成模式的服务器。在本项目中充当消息代理服务器,stomp协议服务端。

    安装:在官网下载,直接解压缩,运行cmd,进入bin目录执行 activemq.bat  start;linux中执行 ./activemq start。

  Stomp:stomp是一个文本定向通讯协议。本项目使用stomp协议基于JavaScript的客户端库 stomp.js

    安装:下载stomp.js,引入项目中

  SockJS:SockJS是WebSocket的JavaScript库,是webSocket的实现  

     安装:下载SockJS.js,引入项目中

 

  第一步 : 创建JavaWeb项目,配置Maven依赖,依赖如下

 

复制代码
 1 <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <spring.version>4.2.4.RELEASE</spring.version>
 4     </properties>
 5 
 6     <dependencies>
 7         <dependency>
 8             <groupId>org.springframework</groupId>
 9             <artifactId>spring-webmvc</artifactId>
10             <version>${spring.version}</version>
11         </dependency>
12 
13         <dependency>
14             <groupId>org.springframework</groupId>
15             <artifactId>spring-websocket</artifactId>
16             <version>${spring.version}</version>
17         </dependency>
18 
19         <dependency>
20             <groupId>org.springframework</groupId>
21             <artifactId>spring-messaging</artifactId>
22             <version>${spring.version}</version>
23         </dependency>
24 
25         <dependency>
26             <groupId>io.projectreactor</groupId>
27             <artifactId>reactor-net</artifactId>
28             <version>2.0.7.RELEASE</version>
29         </dependency>
30 
31         <dependency>
32             <groupId>io.netty</groupId>
33             <artifactId>netty-all</artifactId>
34             <version>4.0.33.Final</version>
35         </dependency>
36 
37 
38         <dependency>
39             <groupId>com.fasterxml.jackson.core</groupId>
40             <artifactId>jackson-databind</artifactId>
41             <version>2.6.4</version>
42         </dependency>
43 
44         <dependency>
45             <groupId>javax.servlet</groupId>
46             <artifactId>javax.servlet-api</artifactId>
47             <version>3.1.0</version>
48             <scope>provided</scope>
49         </dependency>
50 
51 
52     </dependencies>
复制代码

 

  第二步:配置消息终端,在SpringMVC配置文件中加入如下配置

  

复制代码
<websocket:message-broker application-destination-prefix="/app">
        <websocket:stomp-endpoint path="/stomp">
            <websocket:sockjs />
        </websocket:stomp-endpoint>

        <websocket:stomp-broker-relay
            prefix="/topic,/queue"
            relay-host="localhost"
            relay-port="61613"
            heartbeat-receive-interval="20000"
            heartbeat-send-interval="20000"
        />

    </websocket:message-broker>
复制代码

  第三步:编写消息处理代码

  

复制代码
package com.its.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class MessageController {

    @Autowired
    private SimpMessagingTemplate template;

    @MessageMapping("channel")
    public String send(String message){

        String text = message;

        template.convertAndSend("/topic/pinkzhuang",text);

        return text;
    }

}
复制代码

  第四步:编写页面消息收发逻辑,页面要引入stomp.js和SockJS.js

复制代码
function connect(){
            var socket = new SockJS("http://localhost:8080/ActivityMQStomp/stomp");
            stompClient = Stomp.over(socket);
            stompClient.connect({},function(frame){
                setConnected(true);
                console.log("Connected: " + frame);
                stompClient.subscribe("/topic/pinkzhuang",function(greeting){
                    showGreeting(greeting.body);
                });
            });

        }

function disconnect(){
  stompClient.disconnect();
}

function sendMessage(message){
  stompClient.send("/app/vince",{},name);
}
复制代码

 

  第五步:可登录localhost:8161进入activeMQ的管理页面,初始账号密码均为admin,可以手动在管理页面发送消息

  

  The End

posted @ 2018-03-09 15:06  Avatarx  阅读(1083)  评论(0编辑  收藏  举报