在python中为socket.io创建服务器 - 客户端测试

时间:2012-09-25 18:10:51

标签: python websocket socket.io

任何人都可以指出一个完整的新手到我可以学习Python框架来测试socket.io的地方。我非常擅长编写用于测试静态API的脚本,但之前从未使用过WebSocket。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

在gevent-socketIO的示例代码中有大量使用多个流行框架的示例https://github.com/abourget/gevent-socketio/tree/master/examples

答案 1 :(得分:0)

如果您想使用Python与socket.io服务器通信,可以使用socketIO-client

from socketIO_client import SocketIO

def on_bbb_response(*args):
    print 'on_bbb_response', args

with SocketIO('localhost', 8000) as socketIO:
    socketIO.emit('bbb', {'xxx': 'yyy'}, on_bbb_response)
    socketIO.wait_for_callbacks(seconds=1)

答案 2 :(得分:0)

这是我尝试过的。

server.py

let currentInterval = null;

export function fetchInterviewer(lastInterviewerId, interviewId, phone) {
  clearInterval(currentInterval);

  currentInterval = setInterval(()=>{
    fetchInterviewerLoop(lastInterviewerId, interviewId, phone);
  },10000)
}

export function fetchInterviewerLoop(lastInterviewerId, interviewId, phone){
  Store.dispatch(interviewerIsLoading(callId));

  let p = phone && SystemService.decodePhone(phone);

  return {
    type: CALL_FETCH_INTERVIEWER,
    payload: 
    HttpService.get(`${lastInterviewerId}/interview/${interviewId}/interviewer?phone=${p}`)
  }
}

client.py

import json
from aiohttp import web
import socketio

sio = socketio.AsyncServer()

# Creates a new Aiohttp Web Application
app = web.Application()
sio.attach(app)

@sio.on('message')
async def print_message(sid, data):
    print("worked :")
    if data['type'] == "enter":
        print("----- "+data['username'] + " joined the room -----")
    elif data['type'] == "exit":
        print("----- "+data['username'] + " left the room -----")
    else:
        print(data['username']+" > "+data['message'])

if __name__ == '__main__':
    web.run_app(app)