Пример #1
0

# 私聊
@app.route("/o2o/<username>")
def o2o(username):
    user_socket = request.environ.get("wsgi.websocket")  # type: WebSocket
    if user_socket:
        userO2o_socket_dict[username] = user_socket

    while True:
        try:
            msg = user_socket.receive()
            msg_dict = json.loads(msg)

            to_user_link = msg_dict.get("toUser")
            from_user_link = msg_dict.get("fromUser")

            to_user_socket = userO2o_socket_dict.get(to_user_link)
            from_user_socket = userO2o_socket_dict.get(from_user_link)

            to_user_socket.send(msg)
            from_user_socket.send(msg)
        except:
            del userO2o_socket_dict[username]
            continue


if __name__ == "__main__":
    server = WSGIServer(("0.0.0.0", 8527), app, handler_class=WebSocketHandler)
    server.serve_forever()
Пример #2
0
            msg = app_socket.receive()
            msg_info = json.loads(msg)
            receiver = msg_info.get('to_user')
            receiver_socket = web_socket.get(receiver)
            receiver_socket.send(msg)
        except:
            pass


@ws.route('/toy/<toy_id>')
def toy(toy_id):
    toy_socket = request.environ.get('wsgi.websocket')  # type:WebSocket
    web_socket[toy_id] = toy_socket
    print('保持toy_socket连接。。。', toy_socket, toy_id)
    while True:
        try:
            msg = toy_socket.receive()
            msg_info = json.loads(msg)
            receiver = msg_info.get('to_user')
            receiver_socket = web_socket.get(receiver)
            receiver_socket.send(msg)
        except:
            pass


if __name__ == '__main__':
    http_server = WSGIServer(('0.0.0.0', 9528),
                             ws,
                             handler_class=WebSocketHandler)
    http_server.serve_forever()
Пример #3
0
            print("user_msg_dict", user_msg_dict)
            receiver_id = user_msg_dict.get("to_user")
            receiver_socket = socket_dict.get(receiver_id)
            receiver_socket.send(user_msg)
        except geventwebsocket.exceptions.WebSocketError:
            pass


@ws_app.route("/app/<app_id>")
def app_ws(app_id):
    user_socket = request.environ.get("wsgi.websocket")  # type:WebSocket
    if user_socket:
        socket_dict[app_id] = user_socket
    print(len(socket_dict), socket_dict)
    while 1:
        try:
            user_msg = user_socket.receive()
            print(user_msg)
            user_msg_dict = json.loads(user_msg)
            receiver_id = user_msg_dict.get("to_user")
            receiver_socket = socket_dict.get(receiver_id)
            receiver_socket.send(user_msg)
        except geventwebsocket.exceptions.WebSocketError:
            pass


if __name__ == '__main__':
    http_server = WSGIServer(("0.0.0.0", 9528),
                             ws_app,
                             handler_class=WebSocketHandler)
    http_server.serve_forever()
Пример #4
0
        print('ws_li的个数:', len(ws_li))
        for u in ws_li:
            if u == ws:
                continue
            try:
                print(msg, '*' * 8)
                if msg:
                    u.send(msg)
            except:
                dead_li.append(u)
                print('dead_li', len(dead_li))

        print('ws_li为', ws_li)
        print('dead_li的个数', len(dead_li), dead_li)
        import time
        time.sleep(3)
        for i in dead_li:
            ws_li.remove(i)
            dead_li.clear()


@app.route('/ws')
def ws():
    return render_template('wechat.html')


if __name__ == '__main__':
    http_server = WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler)
    http_server.serve_forever()
Пример #5
0
@ws_app.route('/toy/<toy_id>')
def toy(toy_id):
    toy_socket = request.environ.get("wsgi.websocket")  # type:WebSocket
    if toy_socket:
        client_dict[toy_id] = toy_socket
    while True:
        print('toy:', toy_socket)
        print(toy_id)
        toy_data = toy_socket.receive()
        toy_data_dict = json.loads(toy_data)
        to_user = toy_data_dict.get("to_user")
        to_client = client_dict.get(to_user)
        try:
            to_client.send(toy_data)
        except:
            pass


#玩具toy
@ws_app.route('/WebToy')
def index():
    return render_template('WebToy.html')


if __name__ == '__main__':
    http_server = WSGIServer(('0.0.0.0', 9528),
                             application=ws_app,
                             handler_class=WebSocketHandler)
    http_server.serve_forever()
Пример #6
0
from geventwebsocket.handler import WebSocketHandler
from geventwebsocket.server import WSGIServer
from geventwebsocket.websocket import WebSocket

from app import app

http_serv = WSGIServer(("0.0.0.0", 3334), app, handler_class=WebSocketHandler)

if __name__ == "__main__":
    http_serv.serve_forever()
Пример #7
0
from geventwebsocket.server import WSGIServer
from geventwebsocket.handler import WebSocketHandler

from flask import Flask, request

app = Flask(__name__)

socket_dict = {}


@app.route("/app/<username>")
def app_ws(username):
    sock = request.environ.get("wsgi.websocket")  # type:WebSocket
    if not sock:
        return "请使用Websocket客户端连接"
    socket_dict[username] = sock
    print(socket_dict)
    while True:
        msg = sock.receive()
        msg_dict = json.loads(msg)
        receiver = msg_dict.get("receiver")
        recv_sock = socket_dict.get(receiver)
        recv_sock.send(msg)


if __name__ == '__main__':
    http_serv = WSGIServer(("192.168.1.101", 9528),
                           app,
                           handler_class=WebSocketHandler)
    http_serv.serve_forever()
Пример #8
0
    if app_socket:
        user_socket_dict[user_id] = app_socket

    while True:
        app_data = app_socket.receive()
        app_data_dict = json.loads(app_data)
        to_user = app_data_dict.get("to_user")
        to_app_socket = user_socket_dict.get(to_user)
        to_app_socket.send(app_data)


@web_app.route("/toy/<toy_id>")
def toy(toy_id):
    toy_socket = request.environ.get("wsgi.websocket")  # type: WebSocket
    if toy_socket:
        user_socket_dict[toy_id] = toy_socket

    while True:
        toy_data = toy_socket.receive()
        toy_data_dict = json.loads(toy_data)
        to_user = toy_data_dict.get("to_user")
        to_toy_socket = user_socket_dict.get(to_user)
        to_toy_socket.send(toy_data)


if __name__ == "__main__":
    http_serve = WSGIServer(("0.0.0.0", 8000),
                            web_app,
                            handler_class=WebSocketHandler)
    http_serve.serve_forever()
Пример #9
0
@app.route('/query_range')
def query_range():
    parm = request.args.to_dict()
    processfilter = parm.get('processfilter')
    wildcard = ''
    if processfilter is not '':
        wildcard = re.sub(',', "','", "'" + processfilter + "'")
    log.warn(wildcard)
    start = parm.get('start')
    now = time.time()
    if start is None:
        start = now - 300
    end = parm.get('end')
    if end is None:
        end = now
    return DataProcessor.query(start, end, wildcard)


if __name__ == "__main__":
    scheduler_thread = threading.Thread(target=start, args=())
    scheduler_thread.setDaemon(True)
    scheduler_thread.start()
    log.info(u'Scheduler Started.')
    srv = WSGIServer(('0.0.0.0', 5000),
                     app,
                     handler_class=WebSocketHandler,
                     log=log)
    log.info(u'Started Server.')
    srv.serve_forever()
    shutdown()
Пример #10
0
app = Flask(__name__)
user_socket_dict = {}


@app.route('/ws/<nickname>')
def my_ws_func(nickname):
    print(dir(request.environ))
    user_socket = request.environ.get('wsgi.websocket')  # type:WebSocket
    print(user_socket)
    user_socket_dict[nickname] = user_socket
    print(user_socket_dict)
    while 1:
        msg = user_socket.receive()  #等待接收客户端发送的消息
        print(msg)
        msg = json.loads(msg)  # 消息转成Dict
        to_user_socket = user_socket_dict.get(
            msg.get('to_user'))  # 获取要接收消息的那个人
        msg_json = json.dumps(msg)
        to_user_socket.send(msg_json)  #发送消息


@app.route('/one_p')
def one_p():
    return render_template('one_p.html')


if __name__ == '__main__':
    http_serv = WSGIServer(("0.0.0.0", 9527),
                           application=app,
                           handler_class=WebSocketHandler)
    http_serv.serve_forever()
Пример #11
0
def server_start():
    server = WSGIServer(('0.0.0.0', 5000),
                        app,
                        handler_class=WebSocketHandler,
                        log=log)
    server.serve_forever()
Пример #12
0
            msg.ParseFromString(user.receive())
            print(msg.text)
            # user.send(msg.SerializeToString())
            dst = client_list[msg.dst]
            if dst != None:
                dst.send(msg.SerializeToString())
            mongo.message_add(msg)
    except BaseException:
        print('Connection failed')
    
    print('uid {} leave server'.format(uid))
    client_list[uid] = None
    return 'good bye'

if __name__ == '__main__':
    http_server = WSGIServer(('192.168.101.65', 5000), application=app, handler_class=WebSocketHandler)
    http_server.serve_forever()

'''
数据库使用 mongodb 存储
各个界面:
0. / 主界面, 暂无功能
1. /login 登录界面, 负责处理登录请求
2. /post 附近的消息, 请求时返回
3. /setting 设置信息
4. /friend 添加好友
5. /user 查询用户信息
6. /message 获取消息记录

resultCode:
1: 注册用户名重复
Пример #13
0
                return ''
            data = json.loads(message)
            # print(data)
            SOC_RES['code'] = 0
            SOC_RES['data'] = data
            to_user = data.get('to_user')
            if not to_user:
                for name, item in socket_dict.items():
                    if item == user_conn:
                        continue
                    item.send(json.dumps(SOC_RES, ensure_ascii=False))

            else:
                app_conn = socket_dict.get(to_user)
                # print(app_conn)
                try:
                    app_conn.send(json.dumps(SOC_RES, ensure_ascii=False))
                except:
                    continue

    except Exception as r:
        print(r)


if __name__ == '__main__':
    try:
        server = WSGIServer(("0.0.0.0", 8090), application=app01, handler_class=WebSocketHandler)
        server.serve_forever()
    except Exception as e:
        print(e)