2019-05-27:小程序布局分析

2019-05-27  本文已影响0人  AI_Finance
<!--contacts.wxml-->
<view class="container">
  <view class="friends">
    <block wx:for="{{friends}}" wx:key="id">
      <view class="list" id="{{item._id}}" data-friendname="{{item.who}}" bindtap='showDetail'>
        <view class="list_l">
          <image src="{{item['url']}}"></image>
        </view>
        <view class="list_r">
          <text>昵称:{{item['who']}}</text>
          <text>30岁,软件工程师,硕士,{{item['desc']}}</text>
        </view>

        <view wx:if="{{interested==0.0}}">
          <view class="interested">
            <image src="../../images/NoInterested.png"></image>
          </view>
        </view>
        <view wx:elif="{{interested==1.0}}">
          <view class="interested">
            <image src="../../images/Interested.png"></image>
          </view>
        </view>
      </view>
    </block>
  </view>  
</view>

image.png
  <view style='width:100%;height:100%;text-align:center;' class="lovecorner"> 
          <image style='width:70%;border: 10rpx solid #ddd;' src="{{photo}}" mode='widthFix'>
          </image>    
  </view>   

<view class="interest"> 
    <view class="love"> 
        <image src="../../images/love.jpg" mode='widthFix'>
        </image>    
    </view> 
    <view class="donotlove"> 
        <image src="../../images/love.jpg" mode='widthFix'>
        </image>    
    </view>
</view>  

image.png
import threading
import hashlib
import socket
import base64
import struct

global clients
clients = {}


# 通知客户端
def notify(message):
    data = message
    token = b'\x81'
    length = len(data.encode())
    if length <= 125:
        token += struct.pack('B', length)
    elif length <= 0xFFFF:
        token += struct.pack('!BH', 126, length)
    else:
        token += struct.pack('!BQ', 127, length)
    data = token + data.encode()
    for connection in clients.values():
        connection.send(data)


# 客户端处理线程
class websocket_thread(threading.Thread):
    def __init__(self, connection, username):
        super(websocket_thread, self).__init__()
        self.connection = connection
        self.username = username

    def run(self):
        print('new websocket client joined!')
        data = self.connection.recv(1024)
        headers = self.parse_headers(data)
        token = self.generate_token(headers[b'Sec-WebSocket-Key'])
        self.connection.send(b'HTTP/1.1 101 WebSocket Protocol Hybi-10\r\n\
Upgrade: WebSocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Accept: %s\r\n\r\n' % token)
        while True:
            try:
                data = self.connection.recv(1024)
            except socket.error as e:
                print("unexpected error: ", e)
                clients.pop(self.username)
                break
            data = self.parse_data(data)
            if len(data) == 0:
                continue
            message = self.username + ": " + data
            notify(message)

    def parse_data(self, info):
        code_len = info[1] & 0x7f
        if code_len == 0x7e:
            mask = info[4:8]
            decoded = info[8:]
        elif code_len == 0x7f:
            mask = info[10:14]
            decoded = info[14:]
        else:
            mask = info[2:6]
            decoded = info[6:]
        bytes_list = bytearray()
        for i in range(len(decoded)):
            chunk = decoded[i] ^ mask[i % 4]
            bytes_list.append(chunk)
        raw_str = str(bytes_list, encoding="utf-8")
        return raw_str

    def parse_headers(self, msg):
        headers = {}
        header, data = msg.split(b"\r\n\r\n", 1)
        for line in header.split(b"\r\n")[1:]:
            key, value = line.split(b': ', 1)
            headers[key] = value
        headers['data'] = data
        return headers

    def generate_token(self, msg):
        key = msg + b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
        ser_key = hashlib.sha1(key).digest()
        return base64.b64encode(ser_key)


# 服务端
class websocket_server(threading.Thread):
    def __init__(self, port):
        super(websocket_server, self).__init__()
        self.port = port

    def run(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(('127.0.0.1', self.port))
        sock.listen(1)
        print('websocket server started!')
        while True:
            connection, address = sock.accept()
            try:
                username = "ID" + str(address[1])
                thread = websocket_thread(connection, username)
                thread.start()
                clients[username] = connection
            except socket.timeout:
                print('websocket connection timeout!')


if __name__ == '__main__':
    server = websocket_server(9000)
    server.start()

上一篇下一篇

猜你喜欢

热点阅读