Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updates for new NetSocket class

  • Loading branch information...
commit 9fa2099515b151842b6b0bf31238e32e9646ddb7 1 parent 0cb1c63
@jszobody authored
Showing with 20 additions and 16 deletions.
  1. +20 −16 lib/class.talker.php
View
36 lib/class.talker.php
@@ -2,13 +2,13 @@
/*
* This is a real-time PHP client for the Talker group chat application (talkerapp.com).
*
- * Make sure you have PEAR installed (http://pear.php.net), in your include_path, and that
- * the Net_Socket package is installed (http://pear.php.net/net_socket).
+ * Uses the standalone NetSocket class, which is taken and modified from PEAR's
+ * Net_Socket package.
*
* @package Talker.php
* @author Joseph Szobody <jszobody@gmail.com>
*/
-require_once("Net/Socket.php");
+require_once("NetSocket.php");
class Talker {
private $socket;
@@ -19,11 +19,11 @@ class Talker {
private $timeout = 10;
private $connected = false;
- private $user;
- private $users = array();
+ public $user;
+ public $users = array();
function __construct() {
- $this->socket = new Net_Socket();
+ $this->socket = new NetSocket();
}
public function connect($room = "Main", $token) {
@@ -33,9 +33,10 @@ public function connect($room = "Main", $token) {
}
// Connect to server
- if(PEAR::isError($this->socket->connect($this->host, $this->port, false, $this->timeout))) {
+ try {
+ $this->socket->connect($this->host, $this->port, false, $this->timeout);
+ } catch(Exception $e) {
Throw new Exception("Unable to connect");
- return false;
}
$this->socket->enableCrypto(true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
@@ -43,9 +44,10 @@ public function connect($room = "Main", $token) {
// Authenticate
$auth = array("type" => "connect", "room" => $room, "token" => $token);
- if(PEAR::isError($this->send($auth))) {
+ try {
+ $this->send($auth);
+ } catch(Exception $e) {
throw new Exception("Unable to send authentication request");
- return false;
}
$result = json_decode($this->socket->readLine(), true);
@@ -73,9 +75,10 @@ public function send_message($message) {
if(!$this->connected) throw new Exception("Not connected");
$message = array("type" => "message", "content" => $message);
- if(PEAR::isError($result = $this->send($message))) {
- throw new Exception("Unable to send message: " . $result->getMessage());
- return false;
+ try {
+ $result = $this->send($message);
+ } catch(Exception $e) {
+ throw new Exception("Unable to send message: " . $e->getMessage());
}
return true;
}
@@ -85,9 +88,10 @@ public function send_private_message($user_name, $message) {
if(!$this->users[$user_name]) throw new Exception("Invalid user");
$message = array("type" => "message", "content" => $message, "to" => $this->users[$user_name]['id']);
- if(PEAR::isError($result = $this->send($message))) {
- throw new Exception("Unable to send message: " . $result->getMessage());
- return false;
+ try {
+ $result = $this->send($message);
+ } catch(Exception $e) {
+ throw new Exception("Unable to send message: " . $e->getMessage());
}
return true;
}
Please sign in to comment.
Something went wrong with that request. Please try again.