使用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依赖,依赖如下
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.4.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-net</artifactId>
<version>2.0.7.RELEASE</version>
</dependency> <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.33.Final</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.4</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> </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);
}
第五步:管理ActiveMQ
可登录localhost:8161进入activeMQ的管理页面,初始账号密码均为admin,可以手动在管理页面发送消息
The End. (原文)