zoukankan      html  css  js  c++  java
  • websoket的扫码登陆简单用法

     1 import { parseString } from 'xml2js';  //  json与xml相互转化的工具
     2 export default class ConnnectIm {
     3     constructor() {
     4         const ip = process.env.imUri;
     5         const host = "ws://" + ip + ":7070/ws/";
     6         const connection = new WebSocket(host, 'xmpp')
     7         this.connection = connection
     8         this.ip = ip
     9         this.id = null
    10         let firstIn = true
    11         connection.onopen = () => { //建立连接
    12             connection.send(`<open to='${ip}' xmlns='urn:ietf:params:xml:ns:xmpp-framing' xml:lang='zh' version='1.0'/>`)
    13 
    14             connection.send(`<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'>=</auth>`)
    15             connection.onmessage = (res) => { //接受数据
    16                 parseString(res.data, (err, json) => {
    17                     if (json.open) {
    18                         this.id = json.open.$.id;
    19                         connection.send(`<iq type='set' id='${json.open.$.id}'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>`)
    20                     }
    21                     if (json.iq) {
    22                         connection.send(`<presence id="${this.id}"><status>Online</status><priority>1</priority></presence>`)
    23                         if(json.iq.bind[0].jid){
    24                             this.sendJid && this.sendJid(json.iq.bind[0].jid[0])
    25                         }
    26                         
    27                     }
    28                     if (json.message && json.message.body) {
    29                         const body = JSON.parse(json.message.body[0])
    30                         switch (body.ext && body.ext.type) {
    31                             case "userCenterinfo":
    32                                 this.receive && this.receive(body);
    33                                 break;
    34                             default:
    35                                 return
    36                         }
    37                     }
    38                 })
    39             }
    40             connection.onclose = ()=>{ 
    41                 // 关闭 websocket
    42                 console.log("连接已关闭..."); 
    43              };
    44         }
    45     }
    46 
    47     on(action, event) {
    48         this[action] = event.bind(this)
    49     }
    50 
    51 
    52 }
        getQR(){  // 创建img标签将接受后端的二进制流转化为url
            const imconnect = new ImConnet();
            this.setState({
                imconnect
            })
            let that = this;
            const { imgNode } = this.state; //document.createElement('img')
            imconnect.on('sendJid',function(e){
              Ajax.get('getQr',{imJid: e}, (res)=>{   
                const myBlob = new window.Blob([res], {type: 'image/jpeg'})
                const qrUrl = window.URL.createObjectURL(myBlob);  // 生成的带有当前域名的标准blob:链接形式(blob:http://authorization.hqjy.com/4dc453be-bd07-4351-8484-6469f6112b23)
                imgNode.src = qrUrl;
                imgNode.onload = ()=> {
                  window.URL.revokeObjectURL(qrUrl); // 当图片加载完成后释放对象URL.
                }
                that.qrImgBox.appendChild(imgNode); // 将img插入到div容器中
              })
            })
            imconnect.on('receive',function(e){
              console.log(e,'receive')
              const token = e.content; 
              that.setState({
                isOverDue: false,
                isSweeped: true
              })
    // 超过十分钟断开连接,记得释放内存
            setTimeout(()=>{
              imconnect.connection.close();
              this.setState({
                isOverDue: true,
                isSweeped: false
              })
            },1000*60*10)

    参考链接

    https://www.cnblogs.com/liulangmao/p/4262565.html

  • 相关阅读:
    ora-01847:月份中日的值必须介于 1 和当月最后一日之间
    (转)ORACLE中关于外键缺少索引的探讨和总结
    (转) Oracle性能优化-读懂执行计划
    shutdown immediate 持久无法关闭数据库之解决方案
    Oracle11g adump目录下面.aud增长导致空间撑满无法删除导致CRS无法启动的解决方法
    linux几种常见的文件内容查找和替换命令
    unzip解压3G或者4G以上文件失败的解决方法
    IMP-00058: ORACLE error 1882 encountered
    AIX文件系统/var空间100%的问题
    html5手机网站需要加的那些meta/link标签,html5 meta全解(转)
  • 原文地址:https://www.cnblogs.com/lujunan/p/12530445.html
Copyright © 2011-2022 走看看