运行项目目录下的run.bat运行django开发服务器,test.bat是一个使用示例
python调用示例
async def test():
# 连接服务器(先启动服务器:python manage.py runserver 8080)
async with websockets.connect("ws://localhost:8080/ws/s/") as websocket:
# 向服务器发送数据(要监听的直播间url)
await websocket.send(json.dumps({'command': 'https://www.douyu.com/85894'}))
# 接收服务器返回的弹幕数据
while True:
msg = await websocket.recv()
print(msg)
asyncio.run(test())
c#调用示例
public async void WebSocket()
{
try
{
if (Socket != null && Socket.State.Equals(WebSocketState.Open))
{
Debug.Log("不要重复连接");
return;
}
Socket = new ClientWebSocket();
ct = new CancellationToken();
//连接服务器
await Socket.ConnectAsync(new Uri(ServerUrl), ct);
Debug.Log("获取弹幕中...............");
//向服务器发送要获取弹幕的直播间url
GetBulletScreen(LiveRoomUrl);
//获取服务器返回的弹幕数据
while (true)
{
var result = new byte[10240];
await Socket.ReceiveAsync(new ArraySegment<byte>(result), ct);
string msg = Encoding.UTF8.GetString(result, 0, result.Length);
unityEvent?.Invoke(msg);
}
}
catch (Exception e)
{
Debug.Log("连接失败!!");
}
}