-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchatRoom.php
More file actions
106 lines (59 loc) · 1.91 KB
/
chatRoom.php
File metadata and controls
106 lines (59 loc) · 1.91 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
function stream_fifo_open ($fifoPath, $operation) {
$fifo = fopen($fifoPath, $operation) or die("Unable to open file!");
return $fifo;
}
class FIFOCommunication {
public $chatRoom;
public $msgSize;
public $readHead;
public $writeHead;
public function __construct ($_chatRoom = NULL, $_msgSize = 0, $_type = NULL, $_action = NULL) {
$this->chatRoom = $_chatRoom;
$this->msgSize = $_msgSize;
if ($_type === "primary") {
if ($_action === "send") {
$this->writeHead = stream_fifo_open ('temp'.$this->chatRoom.'write.txt', 'w');
} else if ($_action === "recieve"){
$this->readHead = stream_fifo_open ('temp'.$this->chatRoom.'read.txt', 'r');
}
} else {
if ($_action === "send") {
$this->writeHead = stream_fifo_open ('temp'.$this->chatRoom.'read.txt', 'w');
} else if ($_action === "recieve"){
$this->readHead = stream_fifo_open ('temp'.$this->chatRoom.'write.txt', 'r');
}
}
}
public function receive () {
$result = fread($this->readHead, $this->msgSize);
fclose($this->writeHead);
fclose($this->readHead);
echo $result;
}
public function send ($msgToSend) {
fwrite($this->writeHead, $msgToSend);
fclose($this->writeHead);
fclose($this->readHead);
}
public function clear () {
unlink('temp'.$this->chatRoom.'write.txt');
unlink('temp'.$this->chatRoom.'read.txt');
echo 'chat history cleared';
}
}
$_chatRoom = $_REQUEST["chatRoom"];
$_msgSize = $_REQUEST["msgSize"];
$_type = $_REQUEST["type"];
$_action = $_REQUEST["action"];
$_message = $_REQUEST["message"];
$chatObj = new FIFOCommunication($_chatRoom, $_msgSize, $_type, $_action);
if ($_action === "send") {
$chatObj->send($_message);
echo 'message send';
} else if ($_action === "recieve") {
$chatObj->receive();
} else if ($_action === "clear") {
$chatObj->clear();
}
?>