Activite+webSocket+spring实现消息推送(一对一聊天)

 

简介:

该功能我在程自己在学习时候尝试搭建的,可能会有很多问题,在这里只是做一下记录。直接上代码。如有不懂请联系楼主或者加群725395843   这里是技术讨论群。供大家讨论。

功能目的:

实现页面发送消息另一个页面收到消息。并且做数据及时存储等。

废话不说直接上代码:

前端jsp,注意:这里要引用stomp.min.js  在这里不做详细解释下载下来引入进行

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<link rel="stylesheet" type="text/css" href="static/css/style.css" />


</head>
<body>

<div class="workingroom">
    <div class="loginDiv">
    <input id="message"  type="text">
    <input type="button"  class="thisButton" value="发送">
</div>
</body>

<script type="text/javascript" src="/SmallProgram/static/js/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="/SmallProgram/static/js/mask.js"></script>
<script type="text/javascript" src="/SmallProgram/static/js/stomp.min.js"></script>
<script type="text/javascript">
    $(".thisButton").click(function(){
         var url = "ws://127.0.0.1:61614/queue-test";

            var login = "admin";
            
            var passcode = "admin";
            
            //监听的队列
            
            //需要和发送者的发送的队列名称一致否则无法接受到数据
            
            destination = "queue-test";
            
            client = Stomp.client(url);
            
            var onconnect = function(frame) {
            
            client.subscribe(destination, function(message) {
            console.log(message.body);
            alert(message.body)
            alert("有新的订单,请及时处理")
            location.reload();
          });
        };
        client.connect(login, passcode, onconnect);
    })
</script>
<script type="text/javascript">
    $(".thisButton").click(function(){
        var   message =  $("#message").val();
        //发送消息
         $.ajax({
             type: "POST",
             url: "/SmallProgram/WxSmallProgramController/sendWebSocket",
             data: {message,message},
             success:function(Msg){
                alert(“发送成功”);
            }
         });
    })
</script>
</html>

 后台Controller  (将不用的bao删调就可以)

package com.sinosoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinosoft.activiMq.PromoteActProducer;
import com.sinosoft.pojo.Car;
import com.sinosoft.pojo.Card;
import com.sinosoft.pojo.Give;
import com.sinosoft.pojo.Role;
import com.sinosoft.pojo.User;
import com.sinosoft.pojo.UserList;
import com.sinosoft.pojo.UserPage;
import com.sinosoft.service.ProducerService;
import com.sinosoft.service.RoleService;
import com.sinosoft.service.UserDetailsService;
import com.sinosoft.service.UserService;
import com.sinosoft.util.AesCbcUtil;
import com.sinosoft.util.HttpRequest;
import com.sinosoft.util.Msg;

/*
 * 微信访问Demo
 */
@RestController
@RequestMapping("WxSmallProgramController")
public class WxSmallProgramController {
    private Logger logger =  LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserDetailsService userDetailsService ;    
    @Autowired
    UserService userService;
    @Autowired
    private  ProducerService roducerService;

     //测试链接 
    @RequestMapping("webSocket")
    public  ModelAndView    webSocket(Integer user_id){
        ModelAndView model  = new ModelAndView(); 
        model.setViewName("hello");
        return model;
    }
    

   //发送消息 

   @RequestMapping("sendWebSocket")
    public  Msg    sendwebSocket(String   message){
        Destination  destination  =new ActiveMQQueue("张佳鑫");
        roducerService.sendMessage(destination, message);
        Msg  msg  = new Msg();
        return msg;
    }
    
  

}
 

 Service以及实现类

package com.sinosoft.service;

import javax.jms.Destination;

public interface  ProducerService {
     /**
     * 向指定队列发送消息
     */
    void sendMessage(Destination destination, final String msg);
    /**
     * 向默认队列发送消息
     */
    void sendMessage(final String msg);
}
 

 实现类:

package com.sinosoft.service.impl;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.sinosoft.service.ProducerService;

@Service
public class ProducerServiceImpl implements ProducerService {
    @Autowired
    private JmsTemplate jmsTemplate;

    /**
     * 向指定队列发送消息
     */
    public void sendMessage(Destination destination, final String msg) {
        System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });
    }

    /**
     * 向默认队列发送消息
     */
    public void sendMessage(final String msg) {
        String destination =  jmsTemplate.getDefaultDestination().toString();
        System.out.println("向队列" +destination+ "发送了消息------------" + msg);
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });

    }
}

 配置文件:

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.in-memory=false
#true表示使用连接池
spring.activemq.pool.enabled=true
#连接池最大连接数
spring.activemq.pool.max-connections=5
#空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=30000
#强制的连接过期时间,与idleTimeout的区别在于:idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
spring.activemq.pool.expiry-timeout=0
 

    <!-- mq的支持 -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.0</version>
        </dependency>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-pool</artifactId>
        </dependency>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值