-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
91 lines (76 loc) · 2.69 KB
/
server.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
<?php
set_time_limit(0); // prevent the server from timing out
/* * * * * *
*
* Ejemplo configuración en un subdominio en DirectAdmin
* Desde el panel DirectAdmin se le puede poner un certificado
* de LetsEncrypt al subdominio y así podremos conectarnos con
* wss a una url mas o menos como wss://nostr.<domain.tld>/relay
*
* Configuración DNS:
* Zona A : nostr.<domain.tld>. 3600 A <server_ip>
*
* >DirectAdmin
* > Configuraciones Httpd Personalizadas
* > Personalizaciones del Httpd.conf para nostr.<domain.tl>
* (Solamente agregue las pocas lineas del VirtualHost que necesita.
* No agregue todo un <Virtualhost>..</VirtualHost>)
*
* SSLProxyEngine On
* ProxyRequests Off
* ProxyPreserveHost Off
* ProxyVia Full
* <proxy *="">
* Order deny,allow
* Allow from all
* </proxy>
* ProxyPass /relay ws://<server_ip>:9000/
* ProxyPassReverse /relay ws://<server_ip>:9000/
*
*
* * * * */
// Using PHPWebSocket-Chat by Flynsarmy
require 'class.PHPWebSocket.php';
// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server, $friends;
$ip = long2ip( $Server->wsClients[$clientID][6] );
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}
if ( sizeof($Server->wsClients) == 1 ){ // No hay nadie mas
}else{
$decoded = json_decode($message, true);
if($decoded[0]=='EVENT')
$message = '["EVENT","Testing",'.json_encode($decoded[1]).']';
else if($decoded[0]=='EOSE')
$message = '["EOSE","Test","END OF STORED EVENTS"]';
else if($decoded[0]=='REQ')
$message = '["REQ","'.$decoded[1].'",'.json_encode($decoded[2]).']';
else
$message = '["NOTICE","Test","'.print_r($decoded,true).'"]';
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID ) {
$Server->wsSend($id, $message);
}else{
}
}
}
// when a client connects
function wsOnOpen($clientID){
global $Server, $friends, $cfg;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "$ip ($clientID) ha entrado.<br>".print_r($Server->wsClients[$clientID],true) );
}
// when a client closes or lost connection
function wsOnClose($clientID, $status){
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
}
// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
$Server->wsStartServer('0.0.0.0', 9000);