3
中打破了「發射」事件,因爲幾天來我一直試圖在服務器端產生某種循環,這會讓我定期更新客戶端,但是卻沒有成功看起來如果你把一個循環放到服務器事件方法中,它會自動停止向客戶端發送事件。我的直覺是,「gevent」(或greenlet)不允許這種行爲(只有客戶端,使用socket.io的瀏覽器可以定期發送到服務器,而不是相反)。我錯了嗎?你會如何解決這個問題?是否有可能,如果你做一個循環與客戶端(套接字)的連接會以某種方式丟失?我將附加一個與模式的小草案。循環好像在命名空間方法[gevent-socketio]
// Client (socket.io) [Javascript]
client = io.connect('/space');
client.on('do_something', function (msg) {
// Do something.
});
client.on('do_another_thing', function (msg) {
// Do another thing.
});
client.emit('something', msg);
# Server (gevent-socketio) [Python]
@namespace('/space')
class SpaceNamespace:
def on_something(msg):
# This WORKS just fine cause it's out the scope of the loop.
self.emit('do_another_thing', some_operation(msg))
# This DOES NOT work.
while True:
# Each 3 seconds update the client.
self.emit('do_something', some_operation(msg))
time.sleep(3)
# If you put an ipdb here, you can see like the code
# is executed, but the browser doesn't receive any
# event.
謝謝!
在'on_something'方法中是否有兩次調用'self.emit'(沒有任何循環)會導致兩個工作? – sberry
是的,這是之前我試圖使用一個循環,並且一切正常... – javidgon
它可能是值得嘗試轉換到'gevent.sleep(3)' – sberry