summaryrefslogtreecommitdiff
path: root/omegle.py
diff options
context:
space:
mode:
Diffstat (limited to 'omegle.py')
-rw-r--r--omegle.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/omegle.py b/omegle.py
index fd62d45..8c5fdce 100644
--- a/omegle.py
+++ b/omegle.py
@@ -62,9 +62,13 @@ class OmegleChat(RESTClient):
self.timer = Timer(self.events, poll_interval)
self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__ + name)
- self.is_connected = False
+ self.is_confirmed = False #after getting a connection confirm
+ self.is_connected = False #after having self.id
def start(self):
+ if self.is_connected:
+ self.logger.error("Already connected")
+ raise Exception("Already connected")
resp = RESTClient.request(self, "POST", "/start", {})
body=resp.read()
id=body.split("\"")
@@ -73,22 +77,26 @@ class OmegleChat(RESTClient):
self.logger.info("Connected id=%s" % self.id)
#Poll events
self.timer.start()
+ self.is_connected = True
else:
self.logger.error("Bad response: %s" % body)
raise Exception("Bad response: %s" % body)
def disconnect(self):
- self.logger.debug("/disconnect")
+ self.logger.debug("disconnect()")
self.is_connected = False
self.timer.stop()
- r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id}))
- body = r.read()
- self.logger.debug("/disconnect sent. Read: %s" % body)
- if body == "win":
- pass
- else:
- self.is_connected = True #disconnect failed
- raise Exception("/disconnect; Bad response: %s" % body)
+ if self.is_confirmed:
+ self.is_confirmed = False
+ r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id}))
+ body = r.read()
+ self.logger.debug("/disconnect sent. Read: %s" % body)
+ if body == "win":
+ pass
+ else:
+ self.is_connected = True #disconnect failed
+ self.is_confirmed = True
+ raise Exception("/disconnect; Bad response: %s" % body)
def events(self):
"""does use its own "HTTPConnection" because its called async from a thread"""
@@ -141,9 +149,10 @@ class OmegleChat(RESTClient):
elif data[0] == "gotMessage" and data.__len__() == 2:
self.on_message(data[1])
elif data[0] == "connected":
- self.is_connected = True
+ self.is_confirmed = True
self.on_connect()
elif data[0] == "strangerDisconnected":
+ self.is_confirmed = False
self.is_connected = False
self.timer.stop()
self.on_disconnect()