서버에서 클라이언트로 메시지를 보내고 싶습니다. 나는 이걸 보았지만 네임 스페이스 인스턴스를 만들 수 없어서 사용할 수 없다. How to emit SocketIO event on the serverside서버 (django)에서 클라이언트 (socketio)로 알림을 푸시하는 방법은 무엇입니까?
사례보기 : 제품 가격 데이터베이스가 있습니다. 많은 사용자가 현재 내 웹 사이트를 서핑하고 있습니다. 그들 중 일부는 제품 X를보고 있습니다. 서버 측에서 관리자는 제품 가격을 편집 할 수 있습니다. X의 가격을 편집하면 모든 클라이언트는 X 가격이 변경되었다는 알림을보아야합니다 (e.x : 간단한 js 경고). 자바 스크립트 지금
내 클라이언트 :
var socket = io.connect('/product');
#notify server that this client is viewing product X
socket.emit("join", current_product.id);
#upon receive msg from server
socket.on('notification', function (data) {
alert("Price change");
});
내 서버 코드 (socket.py) :
@namespace('/products')
class ProductsNamespace(BaseNamespace, ProductSubscriberMixin):
def initialize(self, *args, **kwargs):
_connections[id(self)] = self
super(ProductsNamespace, self).initialize(*args, **kwargs)
def disconnect(self, *args, **kwargs):
del _connections[id(self)]
super(ProductsNamespace, self).disconnect(*args, **kwargs)
def on_join(self, *args):
print "joining"
def emit_to_subscribers(self): pass
나는이 link에서와 runserver_socketio.py를 사용합니다. (이 우수한 최신 예제는 Calvin Cheng에게 감사드립니다.)
emit_to_subscribers를 호출하는 방법을 모르겠습니다. 이후 네임 스페이스의 인스턴스가 없습니다. 나는이 doc에서 읽을 때 일부 패킷이 그 네임 스페이스를 요청 도착하는 경우에만
네임 스페이스가 만들어집니다.
하지만 어떻게 코드에서 해당 네임 스페이스로 패킷을 보낼 수 있습니까? 클라이언트가 서버에 메시지를 내보낼 때 인스턴스를 만들 수만 있다면 아무도 사이트를 서핑하지 않을 때 가격을 편집 한 직후 시스템이 실패합니다.
정말 네임 스페이스와 인스턴스에 대해 혼란 스럽습니다. 더 명확한 문서가 있으면 나를 도와주세요.
고맙습니다.
이 문제에 대한 더러운 해결 방법이 있습니다. _connections 사전을 사용하여 현재 연결을 추적하십시오. 그런 다음 _connections.items() [0] [1] .emit_to_subscribers()를 사용하십시오. –