-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch WebSocketApp class to make it inheritable
- Loading branch information
Andrey Bashlakov
committed
Jun 18, 2018
1 parent
df275d3
commit f9de412
Showing
4 changed files
with
127 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import websocket | ||
from threading import Thread | ||
import time | ||
import sys | ||
|
||
|
||
class MyApp(websocket.WebSocketApp): | ||
def on_message(self, message): | ||
print(message) | ||
|
||
def on_error(self, error): | ||
print(error) | ||
|
||
def on_close(self): | ||
print("### closed ###") | ||
|
||
def on_open(self): | ||
def run(*args): | ||
for i in range(3): | ||
# send the message, then wait | ||
# so thread doesn't exit and socket | ||
# isn't closed | ||
self.send("Hello %d" % i) | ||
time.sleep(1) | ||
|
||
time.sleep(1) | ||
self.close() | ||
print("Thread terminating...") | ||
|
||
Thread(target=run).start() | ||
|
||
|
||
if __name__ == "__main__": | ||
websocket.enableTrace(True) | ||
if len(sys.argv) < 2: | ||
host = "ws://echo.websocket.org/" | ||
else: | ||
host = sys.argv[1] | ||
ws = MyApp(host) | ||
ws.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters