在开发一个项目的时候使用到了WebSocket协议

  1. 什么是WebSocket?  

      WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。

   2.使用

      • 工具类 

         package com.mz.usps.common.component;
        
         import org.apache.log4j.Logger;
        import org.springframework.stereotype.Component; import javax.websocket.*;
        import javax.websocket.server.PathParam;
        import javax.websocket.server.ServerEndpoint;
        import java.io.IOException;
        import java.util.concurrent.ConcurrentHashMap;
        import java.util.concurrent.ConcurrentMap; //该注解用来指定一个URI,客户端可以通过这个URI来连接到WebSocket。类似Servlet的注解mapping。无需在web.xml中配置。
        @ServerEndpoint("/webSocket/{id}")
        @Component("webSocket")
        public class WebSocket { private static Logger logger = Logger.getLogger(WebSocket.class);
        //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
        private static int onlineCount = 0;
        //与某个客户端的连接会话,需要通过它来给客户端发送数据
        private Session session;
        //concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
        private static ConcurrentMap<String, WebSocket> webSocketMap = new ConcurrentHashMap<>();
        private static ConcurrentMap<String, WebSocket> webSocketMapAdmin = new ConcurrentHashMap<>(); public Session getSession() {
        return session;
        } public static WebSocket getWebSocket(String id) {
        return webSocketMap.get(id);
        } /**
        * 连接建立成功调用的方法
        *
        * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
        */
        @OnOpen
        public void onOpen(Session session, @PathParam("id") String id) {
        this.session = session;
        //String sessionId = session.getId();
        webSocketMap.put(id, this); //加入map中
        if (id.contains("admin")) {// 后台登陆用户,加入list
        webSocketMapAdmin.put(id, this);
        }
        addOnlineCount(); //在线数加1
        System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
        } /**
        * 连接关闭调用的方法
        */
        @OnClose
        public void onClose(@PathParam("id") String id) {
        webSocketMap.remove(id); //从map中删除
        webSocketMapAdmin.remove(id);
        subOnlineCount(); //在线数减1
        System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
        } /**
        * 收到客户端消息后调用的方法
        *
        * @param message 客户端发送过来的消息
        * @param session 可选的参数
        */
        @OnMessage
        public static void onMessage(String message, Session session) {
        //群发消息
        if (webSocketMapAdmin.size() > 0) {
        for (WebSocket item : webSocketMapAdmin.values()) {
        try {
        //System.out.println(item.session.getId());
        item.session.getBasicRemote().sendText(message);
        } catch (IOException e) {
        logger.error("IO异常");
        continue;
        }
        }
        } } /**
        * 发生错误时调用
        *
        * @param session
        * @param error
        */
        @OnError
        public void onError(Session session, Throwable error) {
        //System.out.println("发生错误");
        logger.error("发生错误");
        } /**
        * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
        *
        * @param message
        * @throws IOException
        */
        public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
        //this.session.getAsyncRemote().sendText(message);
        } public static synchronized int getOnlineCount() {
        return onlineCount;
        } public static synchronized void addOnlineCount() {
        WebSocket.onlineCount++;
        } public static synchronized void subOnlineCount() {
        WebSocket.onlineCount--;
        } public static void main(String[] args) {
        /*WebSocket webSocket = new WebSocket();
        System.out.println(webSocket);
        webSocketMap.put("1", webSocket);
        webSocketMap.put("1", webSocket);
        System.out.println(webSocketMap.get("12"));*/
        onMessage("123465",null);
        }
        }
      • 微信登录成功时将用户信息发送给前端
      •  /**
        * 手机端微信登陆
        *
        * @return
        */
        @RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
        @ResponseBody
        public R wxLogin(String userResult, String state, @RequestParam(required = false, value = "userId") String userId) throws Exception {
        //System.out.println(userResult+"\n"+state+"\n"+userId);
        WxUser wxUser = JSON.parseObject(userResult, WxUser.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setUnionId(wxUser.getUnionid());
        userInfo.setOpenid(wxUser.getOpenid());
        userInfo.setSex((long) wxUser.getSex());
        userInfo.setHeadImgUrl(wxUser.getHeadimgurl());
        if (EmojiUtil.containsEmoji(wxUser.getNickName())) {
        logger.info(wxUser.getNickName());
        userInfo.setNickName(EmojiUtil.emojiConverterToAlias(wxUser.getNickName()));
        } else {
        userInfo.setNickName(wxUser.getNickName());
        }
        System.out.println(userInfo.getUnionId());
        UserInfo userInfo1;
        WebSocket webSocket = WebSocket.getWebSocket(state);
        Map<String, Object> m = new HashMap<>();
        //登录
        if (userId == null || "".equals(userId)) {
        if ((userInfo1 = userInfoService.selectWx(userInfo)) != null) {
        //判定账号是否被禁用
        if (userInfo1.getUserState() != 1) {
        R r = new R();
        r.put("code", 500);
        r.put("msg", "此账号因为特殊原因已被禁用,若想重新启用,请联系管理员");
        webSocket.sendMessage(JSON.toJSONString(r));
        return null;
        } userInfo.setUserId(userInfo1.getUserId());
        //System.out.println(userInfo1.getHeadImgUrl()+"你好呀");
        if (userInfo1.getHeadImgUrl() != null) {
        userInfo.setHeadImgUrl(null);
        }
        //System.out.println(userInfo.getHeadImgUrl()+"你好");
        userInfoService.updateByPrimaryKeySelective(userInfo);
        UserInfo userInfo2 = userInfoService.selectWx(userInfo);
        if (EmojiUtil.containsEmoji(userInfo2.getNickName())) {
        userInfo2.setNickName(EmojiUtil.emojiConverterUnicodeStr(userInfo2.getNickName()));
        }
        m.put("openId", userInfo2.getOpenid());
        m.put("userId", userInfo2.getUserId());
        m.put("createTime", new Date());
        String javaWebToken = WebTokenUtil.createJavaWebToken(m);
        userInfo2.setToken(javaWebToken);
        redisCache.setValue(javaWebToken, "1", 1, TimeUnit.DAYS);
        webSocket.sendMessage(JSON.toJSONString(userInfo2));//登录用户信息发送给web
        return R.ok("登录成功");
        } else {
        userInfo.setUserName(userInfo.getNickName());
        userInfoService.insertSelective(userInfo);
        UserInfo userInfo3 = userInfoService.selectWx(userInfo);
        if (EmojiUtil.containsEmoji(userInfo3.getNickName())) {
        userInfo3.setNickName(EmojiUtil.emojiConverterUnicodeStr(userInfo3.getNickName()));
        }
        //判定账号是否被禁用
        if (userInfo3.getUserState() != 1) {
        R r = new R();
        r.put("code", 500);
        r.put("msg", "此账号因为特殊原因已被禁用,若想启用,请联系管理员");
        webSocket.sendMessage(JSON.toJSONString(r));
        return null;
        } System.out.println(userInfo.getOpenid());
        m.put("openId", userInfo3.getOpenid());
        m.put("userId", userInfo3.getUserId());
        m.put("createTime", new Date());
        String javaWebToken = WebTokenUtil.createJavaWebToken(m);
        userInfo3.setToken(javaWebToken);
        redisCache.setValue(javaWebToken, "1", 1, TimeUnit.DAYS);
        webSocket.sendMessage(JSON.toJSONString(userInfo3));//登录用户信息发送给web
        return R.ok("登录成功");
        }
        } else {
        userInfo.setUserId(Long.valueOf(userId));
        //绑定微信前先判断数据库中是否有头像,如果有,不更改头像
        List<UserInfo> selectmemberlist = userInfoService.selectmemberlist(userInfo);
        if (selectmemberlist.get(0).getHeadImgUrl() != null) {
        userInfo.setHeadImgUrl(null);
        } UserInfo userInfo2 = userInfoService.selectWx(userInfo);
        if (userInfo2 != null) {
        R r = new R();
        r.put("code", 500);
        r.put("msg", "此微信已被注册,若确认是你本人微信,请联系管理员帮你更改");
        webSocket.sendMessage(JSON.toJSONString(r));
        return R.error("");
        }
        if (EmojiUtil.containsEmoji(wxUser.getNickName())) {
        userInfo.setNickName( EmojiUtil.emojiConverterToAlias(wxUser.getNickName()));
        }
        R r = userInfoService.updateByPrimaryKeySelective(userInfo);
        if (r.get("code").equals(200)) {
        R r1 = new R();
        r1.put("code", 200);
        r1.put("msg", "更改成功");
        webSocket.sendMessage(JSON.toJSONString(r1));
        } else {
        R r2 = new R();
        r2.put("code", 500);
        r2.put("msg", "更改失败");
        webSocket.sendMessage(JSON.toJSONString(r2));
        }
        return r;
        }
        } }

Java后台使用Websocket教程的更多相关文章

  1. WebSocket实现Java后台消息推送

    1.什么是WebSocket WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端. 2.实现原理 在实现 ...

  2. fastJson java后台转换json格式数据

    什么事JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Progra ...

  3. Java 后台创建word 文档

    ---恢复内容开始--- Java 后台创建 word 文档 自己总结  网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...

  4. 坚持:学习Java后台的第一阶段,我学习了那些知识

    最近的计划是业余时间学习Java后台方面的知识,发现学习的过程中,要学的东西真多啊,让我一下子感觉很遥远.但是还好我制定了计划,自己选择的路,跪着也要走完!关于计划是<终于,我还是下决心学Jav ...

  5. 微信小程序与java后台交互

    java后台使用的ssm框架,小程序连接的本地接口.跟正常的web访问没什么区别,也是后台获取url,返回json数据:只是小程序前台请求的url要带上http://localhost:80801. ...

  6. 【分享】Java后台开发精选知识图谱

    地址 引言: 学习一个新的技术时,其实不在于跟着某个教程敲出了几行.几百行代码,这样你最多只能知其然而不知其所以然,进步缓慢且深度有限,最重要的是一开始就对整个学习路线有宏观.简洁的认识,确定大的学习 ...

  7. 非科班双非本科投的337家Java后台(励志)

    考试结束,班级平均分只拿到了年级第二,班主任于是问道:大家都知道世界第一高峰珠穆朗玛峰,有人知道世界第二高峰是什么吗?正当班主任要继续发话,只听到角落默默想起来一个声音:”乔戈里峰” 前言 文章出自h ...

  8. WebSocket 教程(转载)

    WebSocket 教程   作者: 阮一峰 日期: 2017年5月15日 WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebSocket 协议的使用方法. 一.为什么需 ...

  9. form表单提交中文乱码(前台中文到JAVA后台乱码)问题及解决

    form表单提交中文乱码(前台中文到JAVA后台乱码)问题及解决 一.问题: 页面输入框中的中文内容,在后台乱码,导致搜索功能失效:(详细可以见后面的重现) 二.原因: 浏览器对于数据的默认编码格式为 ...

随机推荐

  1. LeetCode第十九题-链表节点的删除

    Remove Nth Node From End of List 问题简介;给定链表,从链表末尾删除第n个节点并返回其头部 例: 给定链表:1-> 2-> 3-> 4-> 5, ...

  2. Eclipse 配置Tomcat 服务器

    第一部分:eclipse环境下如何配置tomcat 1.下载并成功安装Eclipse和Tomcat 2.打开Eclipse,单击“window”菜单,选择下方的“Preferences” . 选择好自 ...

  3. Django—模板

    索引 一.模板语言 1.1 变量 1.2 标签 1.3 过滤器 1.4 自定义过滤器 1.5 注释 二.模板继承 三.HTML转义 四.CSRF 五.验证码 六.反向解析 模板 作为Web框架,Dja ...

  4. 预制体,Mask组件

    1.预制体制作和使用 a.制作预制体,将制作好的元素插入到在文件夹下形成一个预制体 b.将预制体在所调用的脚本文件中进行声明,并且在界面里进行拖入保存 c.使用的时候利用cc.instantiate进 ...

  5. 洛谷 P1111 修复公路

    题目链接 https://www.luogu.org/problemnew/show/P1111 以后只发题目链接!!! 题目大意 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连 ...

  6. iOS开发之zip文件解压

    今天给大家分享zip解压到指定目录 首先需要下载ZipArchive文件 下载地址:https://pan.baidu.com/s/1S6qYicoVr3M3hI0M1EW2Bw 将下载的文件导入工程 ...

  7. 【玩转开源】基于Docker搭建Bug管理系统 MantisBT

    环境Ubuntu18.04 + Docker 1. Docker Hub 链接:https://hub.docker.com/r/vimagick/mantisbt 这里直接使用docker命令的方式 ...

  8. ***新版微信H5支付技术总结(原创)

    新版微信H5支付官方文档: https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1 H5支付是指商户在微信客户端外 ...

  9. ***CodeIgniter框架集成支付宝即时到账支付SDK

    本文为CI集成支付宝即时到账支付接口 1.下载支付宝官方demo ;即时到账交易接口(create_direct_pay_by_user)(DEMO下载) 原文地址:https://doc.open. ...

  10. pyqt win32发送QQ消息

    标题应该改为:python+win32发送QQ消息,全程使用python套个pyqt壳. 其实代码来自: http://blog.csdn.net/suzyu12345/article/details ...