我正在构建一个简单的聊天应用程序,并且一直在尝试通过Flask-SocketIO向房间发送消息。
来自客户端的“ mesage_event”事件很好地到达了服务器,但是然后,我在客户端看不到任何东西。我不知道服务器是否向房间发出任何东西,但在客户端,控制台中看不到任何东西。 我只能通过广播成功发送给所有客户端。
这是我在服务器端的代码:
@socketio.on("send msg")
def sendsocket(data):
print("send msg start:", data["msg"])
msg = data["msg"]
room = data["room"]
emit("message_event", msg, room = room)
和客户端:
1-用于发送消息:
socket.emit('send msg', {'msg': msg, 'room': room})
2-用于触发事件处理程序:
socket.on('message_event', data => {
console.log("message received:", data);
});
答案 0 :(得分:2)
您缺少2件事。
第一,您需要向服务器发送void CMyImageView::OnDraw(CDC* pDCView)
{
if (m_DIBData != NULL) {
#define PALSIZE(x) ((x) > 8? 0: 1 << (x))
// move different versions of bitmap header to bitmap structure
BITMAP bm;
if (m_DIBData->biSize == sizeof(BITMAPCOREHEADER)) {
bm.bmWidth=((BITMAPCOREHEADER*)m_DIBData)->bcWidth;
bm.bmHeight=((BITMAPCOREHEADER*)m_DIBData)->bcHeight;
bm.bmBitsPixel=((BITMAPCOREHEADER*)m_DIBData)->bcBitCount;
bm.bmType=BI_RGB;
bm.bmBits=PBYTE(m_DIBData) + m_DIBData->biSize + PALSIZE(bm.bmBitsPixel) * 3;
}
else {
bm.bmWidth=m_DIBData->biWidth;
bm.bmHeight=m_DIBData->biHeight;
bm.bmBitsPixel=m_DIBData->biBitCount;
bm.bmType=m_DIBData->biCompression;
long color=m_DIBData->biClrUsed ? m_DIBData->biClrUsed : PALSIZE(bm.bmBitsPixel);
bm.bmBits=PBYTE(m_DIBData) + m_DIBData->biSize + color * 4;
}
CPoint point=GetScrollPosition();
CRect rcClient(0, 0, 0, 0);
if(pDCView->IsPrinting())
{
pDCView->SetMapMode(MM_ISOTROPIC);
// setup base size of item
pDCView->SetWindowExt(bm.bmWidth, bm.bmHeight);
rcClient.right=bm.bmWidth;
rcClient.bottom=bm.bmHeight;
// setup view output to full page
int horzres=pDCView->GetDeviceCaps(HORZRES);
int vertres=pDCView->GetDeviceCaps(VERTRES);
pDCView->SetViewportExt(horzres, vertres);
}
else {
ASSERT(MM_TEXT == pDCView->GetMapMode());
ASSERT(CPoint(0, 0) == pDCView->GetViewportOrg());
GetClientRect(rcClient);
}
const int cx=rcClient.right; // view client area width
const int cy=rcClient.bottom; // view client area height
const int bx=bm.bmWidth; // source bitmap width
const int by=bm.bmHeight; // source bitmap height
const int vx=(int)(bx * m_fZoom); // virtual document width
const int vy=(int)(by * m_fZoom); // virtual document height
const int xPos=point.x; // horizontal scroll position
const int yPos=point.y; // vertical scroll position
// source and destination coordinates and sizes
int xSrc, ySrc, nSrcWidth, nSrcHeight, xDst, yDst, nDstWidth, nDstHeight;
if (vx > cx) {
xSrc=(int)(xPos / m_fZoom);
nSrcWidth=bx - xSrc;
xDst=0;
nDstWidth=vx - xPos;
}
else {
xSrc=0;
nSrcWidth=bx;
xDst=cx / 2 - vx / 2;
nDstWidth=vx;
}
if (vy > cy) {
ySrc=0;
nSrcHeight=by;
yDst=-yPos;
nDstHeight=vy;
}
else {
ySrc=0;
nSrcHeight=by;
yDst=cy / 2 - vy / 2;
nDstHeight=vy;
}
::StretchDIBits(pDCView->m_hDC,
xDst, yDst, // xy-coordinates of upper-left corner of dest. rect.
nDstWidth, nDstHeight, // width & height of destination rectangle
xSrc, ySrc, // xy-coordinates of lower-left corner of source rect.
nSrcWidth, nSrcHeight, // width & height of source rectangle
bm.bmBits, // address of array with DIB bits
(BITMAPINFO*)m_DIBData, // address of structure with bitmap info
DIB_RGB_COLORS, // usage flags
SRCCOPY); // raster operation code
}
}
事件。
"join"
例如,在这里我将触发器附加到按钮上。为了使最初测试将用户添加到房间更容易,我使用<script>
function joinRoom() {
console.log("ask server to join room");
socket.emit("join", { "user": Date.now(), "room": "Notifications" });
}
</script>
<body>
<button onclick="joinRoom()">Join</button>
</body>
作为用户名。您可以打开不同的标签来充当不同的用户。
第二个,您需要为该Date.now()
事件提供处理程序。
Flask-SocketIO文档的Rooms and Namespaces部分中有一个示例。
join
在处理程序中,您需要调用join_room
方法以将用户添加到当前名称空间下的房间 中。请注意有关名称空间的那部分。默认情况下,所有连接都在根(@socketio.on("join")
def on_join(data):
user = data["user"]
room = data["room"]
print(f"client {user} wants to join: {room}")
join_room(room)
emit("room_message", f"Welcome to {room}, {user}", room=room)
)命名空间下。如果您有自定义名称空间,则每个名称空间都将有自己的房间。
还有一种相应的leave_room
方法。
这是完整的服务器端代码:
/
这是完整的客户端代码:
@socketio.on("connect")
def connect():
print("client wants to connect")
emit("status", { "data": "Connected. Hello!" })
@socketio.on("join")
def on_join(data):
user = data["user"]
room = data["room"]
print(f"client {user} wants to join: {room}")
join_room(room)
emit("room_message", f"Welcome to {room}, {user}", room=room)
现在,您现在可以打开多个选项卡并将每个选项卡连接到服务器。服务器端应显示以下消息:
<script type="text/javascript" charset="utf-8">
const socket = io();
socket.on("connect", () => {
console.log("connect");
});
socket.on("status", (status) => {
console.log("received status: " + status.data);
});
socket.on("room_message", (msg) => {
console.log("message from room: " + msg);
});
function joinRoom() {
console.log("ask server to join room");
socket.emit("join", { "user": Date.now(), "room": "Notifications" });
}
</script>
<body>
<button onclick="joinRoom()">Join</button>
</body>
在第一个加入会议室的用户(1582428076724)上,您应该能够看到其他用户加入会议室的日志。
client wants to connect
client wants to connect
client wants to connect
client 1582428076724 wants to join: Notifications
client 1582428080023 wants to join: Notifications
client 1582428082916 wants to join: Notifications