WebSockets
Met de nieuwe WebSocket in HTML5 kun je nu bijvoorbeeld makkelijk een efficiente chat maken.
Als ik het goed begrijp, kan door die websockets in HTML nu ook een server iets sturen naar een client. Ik weet niet of dat vroeger ook al kon toen er nog geen HTML5 was. Met alleen maar php en html met javascript kon je vroeger alleen een chat maken door steeds met javascript om de x aantal seconden een request te sturen naar de server.
Ik heb nu de volgende code, maar hij werkt niet. Ik weet zeker dat de socket functies worden ondersteund op de server.
socket.php
Code (php)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
class WebSocket {
private $socket;
private $msg_socket;
private $host;
private $type;
private $protocol;
private $adress;
private $port;
private $queueLength;
private $errors = array();
/**
* @param host - Default localhost
* @param port - Default 8000
* @param protocol - Default 2 (tcp)
*/
public function __construct($host = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP) {
$this->create($host, $type, $protocol);
}
public function prepareSockets() {
set_time_limit(10);
ob_implicit_flush();
}
public function create($host = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP) {
$this->host = $host;
$this->type = $type;
$this->protocol = $protocol;
$this->socket = socket_create($this->host, $this->type, $this->protocol);
if($this->socket === false) {
$this->addError("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
}
}
public function bind($adress, $port) {
$this->adress = $adress;
$this->port = $port;
if(socket_bind($this->socket, $this->adress, $this->port) === false) {
$this->addError("socket_bind() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
}
}
public function listen($queueLength = 5) {
$this->queueLength = $queueLength;
if(socket_listen($this->socket, $this->queueLength) === false) {
$this->addError("socket_listen() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
}
}
public function accept() {
$this->msg_socket = socket_accept($this->socket);
if($this->msg_socket === false) {
$this->addError("socket_accept() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
return false;
}
}
public function write($msg) {
$write = socket_write($this->msg_socket, $msg, strlen($msg));
if($write === false) {
$this->addError("socket_write() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
return false;
}
return $write;
}
public function closeMsgSocket() {
socket_close($this->msg_socket);
}
public function closeSocket() {
socket_close($this->socket);
}
public function addError($str) {
$this->errors[] = $str;
}
public function getErrors() {
return $this->errors;
}
}
?>
class WebSocket {
private $socket;
private $msg_socket;
private $host;
private $type;
private $protocol;
private $adress;
private $port;
private $queueLength;
private $errors = array();
/**
* @param host - Default localhost
* @param port - Default 8000
* @param protocol - Default 2 (tcp)
*/
public function __construct($host = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP) {
$this->create($host, $type, $protocol);
}
public function prepareSockets() {
set_time_limit(10);
ob_implicit_flush();
}
public function create($host = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP) {
$this->host = $host;
$this->type = $type;
$this->protocol = $protocol;
$this->socket = socket_create($this->host, $this->type, $this->protocol);
if($this->socket === false) {
$this->addError("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
}
}
public function bind($adress, $port) {
$this->adress = $adress;
$this->port = $port;
if(socket_bind($this->socket, $this->adress, $this->port) === false) {
$this->addError("socket_bind() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
}
}
public function listen($queueLength = 5) {
$this->queueLength = $queueLength;
if(socket_listen($this->socket, $this->queueLength) === false) {
$this->addError("socket_listen() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
}
}
public function accept() {
$this->msg_socket = socket_accept($this->socket);
if($this->msg_socket === false) {
$this->addError("socket_accept() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
return false;
}
}
public function write($msg) {
$write = socket_write($this->msg_socket, $msg, strlen($msg));
if($write === false) {
$this->addError("socket_write() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
return false;
}
return $write;
}
public function closeMsgSocket() {
socket_close($this->msg_socket);
}
public function closeSocket() {
socket_close($this->socket);
}
public function addError($str) {
$this->errors[] = $str;
}
public function getErrors() {
return $this->errors;
}
}
?>
message.php
Code (php)
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
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
<?php
include "socket.php";
set_time_limit(0);
ob_implicit_flush();
$s = new WebSocket();
$s->bind("localhost", 8000);
$s->listen(5);
echo "Test";
print_r($s->getErrors());
$s->write("Test socket");
while(true) {
if($s->accept() === false) {
break;
}
$s->write("PHP: Test text");
}
print_r($s->getErrors());
?>
include "socket.php";
set_time_limit(0);
ob_implicit_flush();
$s = new WebSocket();
$s->bind("localhost", 8000);
$s->listen(5);
echo "Test";
print_r($s->getErrors());
$s->write("Test socket");
while(true) {
if($s->accept() === false) {
break;
}
$s->write("PHP: Test text");
}
print_r($s->getErrors());
?>
Als er php errors zijn, dan stopt het script en worden die errors gewoon door php getoond.
Als er (na mijn weten) geen errors zijn, dan laadt het script, best wel lang totdat ik een gateway timeout krijg (status 504)
Gewijzigd op 29/07/2014 21:59:11 door Mark Hogeveen
Er zijn nog geen reacties op dit bericht.