-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCPclient.php
More file actions
executable file
·85 lines (70 loc) · 2.68 KB
/
TCPclient.php
File metadata and controls
executable file
·85 lines (70 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* TCP Implementation - socket-tcp-php-client
*
* create new TCPClient
* connection socket to server
* sending message to server
* implement callback for receive message from server
*
* @author M Sofiyan
* @email msofyancs@gmail.com
* @skypeid viyancs
* if you want to using part of full this code, please don't remove this comment
*
* */
define('__LIB__', dirname(dirname(__FILE__)));
require_once(__LIB__ . '/TCP/library/TCPConnection.php');
require_once(__LIB__ . '/TCP/library/TCPCallback.php');
class TCPClient implements TCPCallback {
public $socket;
public function index() {
$this->socket = new TCPConnection("127.0.0.1", 1338, $this);
$this->socket->connect(); //connection to socket
$data = array("username" => "foo", "pass" => "bar");
//TCPConnection::send("testing socket TCP dari viyancs"); // format string only
//TCPConnection::emit("login", array((object)($data), (object)($data1))); //multy dimension json
//$this->socket->emit("login", array($data)); //one array dimension json
TCPConnection::emit("login", array($data));
}
public function onConnect() {
echo "socket is connected \n";
echo "-------------------------------------------------------------\n";
}
public function onDisconnect() {
echo "socket is disconnected \n";
echo "-------------------------------------------------------------\n";
}
public function onError($err) {
echo "something wrong : " . $err .'\n';
}
public function onJSON($json) {
echo "receive data from server \n";
echo "=============================================================\n";
echo "the data is " . $json . '\n';
echo "=============================================================\n";
}
public function onJSONEvent($event, $jsonArray) {
echo "receive data from server </br>";
echo "=============================================================\n";
echo "the event is " . $event . '\n';
foreach ($jsonArray as $key => $value) {
echo "Key: $key; \n";
var_dump($value);
echo '\n';
}
echo "=============================================================\n";
}
public function onMessage($message) {
echo "message from sever :" . $message .'\n';
}
public function onSend($msg) {
echo "sending data to server \n";
echo "=============================================================\n";
echo 'data = ' . $msg . '\n';
echo "=============================================================\n";
}
}
$client = new TCPClient();
$client->index();
?>