使用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. (原文

    

ActiveMQ、Stomp、SockJS入门级应用的更多相关文章

  1. php 利用activeMq+stomp实现消息队列

    php 利用activeMq+stomp实现消息队列 一.activeMq概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J ...

  2. activemq stomp类

    此库用来增强ide,能对stomp类进行自动提示 <?php class Stomp { /** * 构造器 * Stomp constructor. * @param string $brok ...

  3. spring+rabbitmq+stomp搭建websocket消息推送(非spring boot方式)

    前言: 两年前做过spring+activemq+stomp的ws推送,那个做起来很简单,但现在公司用的mq中间件是rabbitmq,因此需要通过rabbitmq去做ws通信.仔细搜了搜百度/谷歌,网 ...

  4. centos下的activemq的配置及PHP的使用

    一.安装JDK 1.下载JDK(官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...

  5. Golang 实现UDPServer并发送消息到ActiveMQ

    示例代码 package main import ( "net" "os" "github.com/gpmgo/gopm/modules/goconf ...

  6. springboot2 -广播式WebSocket

    1.WebSocket,STOMP,SockJS含义 WebSocket:WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. SockJS:SockJS 是 We ...

  7. 006-优化web请求二-应用缓存、异步调用【Future、ListenableFuture、CompletableFuture】、ETag、WebSocket【SockJS、Stomp】

    四.应用缓存 使用spring应用缓存.使用方式:使用@EnableCache注解激活Spring的缓存功能,需要创建一个CacheManager来处理缓存.如使用一个内存缓存示例 package c ...

  8. Spring+Stomp+ActiveMq实现websocket长连接

    stomp.js+spring+sockjs+activemq实现websocket长连接,使用java配置. pom.xml(只列出除了spring基本依赖意外的依赖,spring-version为 ...

  9. ActiveMQ使用STOMP协议的一个错误问题:Unexpected ACK received for message-id

    使用某些语言环境下的stomp包(比如php python ruby),可能会出现如下问题: Unexpected ACK received for message-id 这一般可能有两个原因. 1. ...

随机推荐

  1. This is probably because there is no OLE editor registered against the type of file you were trying to open.

    Reason: This is probably because there is no OLE editor registered against the type of file you were ...

  2. ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(二)—启用用户管理

    修改和启用默认的用户账户管理和角色管理 一.修改Models目录中的ApplicationUser.cs类文件,如下 namespace xxxx.Models{    //将应用程序用户的属性添加到 ...

  3. Altium designer知识总结

    原理图库文件 =原理图元件库 .schlibPCB库文件=封装库.pcblib 集成库=原理图库文件+封装库 .intlib

  4. PID算法

    所谓PID就是比例-积分-微分的英文缩写,但并不是必须同时具备这三种算法,也可以是 PD, PI,甚至只有 P算法控制,下面分别介绍每个参数的含义:首先需要明确一个事实就是,要实现PID算法,必须在硬 ...

  5. java连接mysql以及增删改查操作

    java连接数据库的代码基本是固定的,步骤过程觉得繁琐些,代码记起来对我来说是闹挺.直接上代码: (温馨提醒:你的项目提前导入连接数据库的jar包才有的以下操作 ) class DBConnectio ...

  6. junit设计模式--组合模式

    Composite,英语翻译下,复合,组合. 组合模式有时候又叫做部分-整体模式,它使我们在树形结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户 ...

  7. Linux指令--ln

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...

  8. maven系列--maven目录

    我们在玩maven,首先就是利用maven来管理我们的项目.其实maven并不难,它无非是一种目录结构.所以在本系列开始之前,我们要细致的了解下maven的目录,其实也就是maven的约定. 约定优于 ...

  9. MySQL中时间函数NOW()和SYSDATE()的区别

    mysql中日期函数还是比较常用的.主要有NOW()和SYSDATE()两种,虽然都表示当前时间,但使用上有一点点区别. NOW()取的是语句开始执行的时间,SYSDATE()取的是动态的实时时间. ...

  10. java获取昨天的日期

    Calendar   cal   =   Calendar.getInstance();  cal.add(Calendar.DATE,   -1);  String yesterday = new ...