用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
  • TA的每日心情
    慵懒
    2017-1-6 10:46
  • 签到天数: 2 天

    [LV.1]初来乍到

    173

    主题

    182

    帖子

    945

    金钱

    咫尺尊享

    Rank: 5Rank: 5

    积分
    0
    2017-3-23 11:33:32 柿霖不是林 咫尺尊享 楼主 664429




    [HTML] 纯文本查看 复制代码
    // room.js
    // var util = require('../../utils/util.js')
    var WxParse = require('../../wxParse/wxParse.js');
    var roomConfig = {
      wsurl: 'wss://yourdomain.com/ws',
      nickname: Math.random().toString(36).substr(2, 4),
      channel: 'wx',
    }
    var mynickname = roomConfig.nickname;
    var online = [];
    var initMsg = '<i class="msg info">☼ Welcome ~ </i>\n';
    var extraMsgs = [];
    var socketOpen = false;
    var socketMsgQueue = [{cmd: 'join', channel: roomConfig.channel, nick: roomConfig.nickname }];
    var parseHtml = function (text) {
      //html = html.replace(/<a.+?href="(.+?)".*?>(.+?)<\/a>/ig,'[url=$1]$2[/url]');
      text = text.replace(/(https?:\/\/[^\s]+)/g, function (link) {
        return '<a href="' + link + '">网址</a> \n';
      });
      return text;
    };
     
    Page({
      data: {
        scrollTop: 1000000000000,
        boxHeihgt: 400,
        inputValue: '',
        messages: ''
      },
      bindKeyInput: function(e) {
        this.setData({
          inputValue: e.detail.value
        })
      },
      sendBtn: function () {
        this.sendMsg(this.data.inputValue)
        this.setData({
          inputValue: ''
        });
      },
      sendMsg: function (msg) {
        wx.sendSocketMessage({
          data: JSON.stringify({cmd: 'chat', text: msg})
        });
      },
      getMsg: function(data) {
     
        var args = JSON.parse(data);
        //Chat
        if (args.cmd === 'chat') {
          if (args.trip == undefined) {
            args.trip = "(No trip)";
          }
          var isMe = '';
          if (args.nick == mynickname.split("#")[0]) {
            isMe = ' me';
          }
          // extraMsgs.push("\n" + "\033[F" + args.trip + " " + args.nick + ": " + args.text);
          extraMsgs.push('<div class="msg chat'+isMe+'">◦<em class="nick">'  + args.nick + ":</em> " + args.text +'</div>');
          console.log(args);
        }
     
        //Messages Process
        switch (args.cmd) {
          case 'warn':
            if (args.text === "Nickname taken") {
              extraMsgs.push('<div class="msg info">'+ args.text + '</div>');
              return false;
            }
            console.log("Your IP is being rate-limited or blocked.");
            break;
          case 'onlineSet':
            for (var i = 0; i < args.nicks.length; i++) {
                online.push(args.nicks[i]);
            }
            extraMsgs.push("\n" + '<div class="msg info">当前在线('+online.length+'): ' + online + '</div>');
            break;
          case 'onlineAdd':
            online.push(args.nick);
            extraMsgs.push('<div class="msg info"><em class="nick">'+ args.nick +  '</em> 进入了大厅</div>');
            break;
          case 'onlineRemove':
            var x = online.indexOf(args.nick);
            if (x != -1) {
                online.splice(x, 1);
                extraMsgs.push('<div class="msg info"><em class="nick">'+ args.nick + '</em> 离开了大厅</div>');
            }
            break
          default:
        }
     
        this.setData({
          messages: initMsg + extraMsgs.join('\n')
        });
     
        this.setData({scrollTop:this.data.scrollTop+100});
     
        WxParse.wxParse('messages', 'html', parseHtml(this.data.messages), this, 5);
     
      },
      onLoad: function () {
        var that = this;
     
        wx.getSystemInfo({
          success: function(res) {
            that.setData({
              boxHeihgt: res.windowHeight-90
            })
          }
        });
        wx.connectSocket({
          url: roomConfig.wsurl
        });
     
        wx.onSocketOpen(function(res) {
          console.log('WebSocket连接已打开!')
          socketOpen = true
          for (var i = 0; i < socketMsgQueue.length; i++){
             sendSocketMessage(socketMsgQueue[i])
          }
          socketMsgQueue = []
        });
     
        function sendSocketMessage(msg) {
          if (socketOpen) {
            wx.sendSocketMessage({
              data: JSON.stringify(msg)
            })
          } else {
             socketMsgQueue.push(msg)
          }
        };
     
        wx.onSocketMessage(function(res) {
          // console.log('收到服务器内容:' + res.data)
          that.getMsg(res.data)
        });
     
        setTimeout(function() {
          wx.sendSocketMessage({
            data: JSON.stringify({cmd: 'ping'})
          });
          console.log(101);
        }, 50000);
     
      }
    })



    游客,如果您要查看本帖隐藏内容请回复

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    使用道具 举报 回复
    这个有点意思哦,可以看看再回复
    学习666666
    哈哈!66
    使用道具 举报 回复
    确实是难得好帖啊,顶先
    啥也不说了,感谢楼主分享哇!
    如果我想做个能跳转到指定账号的小程序,是不是很难啊
    确实是难得好帖啊,顶先
    正需要,支持楼主大人了!
    正需要,支持楼主大人了!
    发新帖
    *滑动验证:
    您需要登录后才可以回帖 登录 | 立即注册
    咫尺论坛|小程序开发社区
    X