'PHP - Chat Socket working on localhost but not working on dedicated server
The chat program runs fine while on localhost but when uploaded to server gives the error while connecting.
server file not working on the server, shows error while executing the code
$server = new Ratchet\App('wss://example.in', 8080);
Sorry, im still new to the ratchet library and still trying to work out how to achieve this task.
The code from server.php file
<?php
set_time_limit(0);
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once '../vendor/autoload.php';
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onMessage(ConnectionInterface $from, $data) {
$from_id = $from->resourceId;
$data = json_decode($data);
$type = $data->type;
switch ($type) {
case 'chat':
$user_id = $data->user_id;
$chat_msg = $data->chat_msg;
$response_from = "<span style='color:#999'><b>".$user_id.":</b> ".$chat_msg."</span>";
$response_to = "<b>".$user_id."</b>: ".$chat_msg."";
// Output
$from->send(json_encode([
"type"=>$type,"msg"=>$response_from
]));
foreach($this->clients as $client) {
if($from!=$client) {
$client->send(json_encode([
"type"=>$type,"msg"=>$response_to
]));
}
}
break;
}
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
$server = new Ratchet\App('wss://example.in', 8080);
$server->route('/', new Chat, ['*']);
$server->run();
?>
Code from index.php
<?php $session = mt_rand(1,999); ?>
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="js/jquery.js" type="text/javascript"></script>
<style type="text/css">
* {margin:0;padding:0;box-sizing:border-box;font-family:arial,sans-serif;resize:none;}
html,body {width:100%;height:100%;}
#wrapper {position:relative;margin:auto;max-width:1000px;height:100%;}
#chat_output {position:absolute;top:0;left:0;padding:20px;width:100%;height:calc(100% - 100px);}
#chat_input {position:absolute;bottom:0;left:0;padding:10px;width:100%;height:100px;border:1px solid #ccc;}
</style>
</head>
<body>
<div id="wrapper">
<div id="chat_output"></div>
<textarea id="chat_input" placeholder="Deine Nachricht..."></textarea>
<script type="text/javascript">
jQuery(function($){
// Websocket
var websocket_server = new WebSocket("wss://example.in:8080/");
websocket_server.onopen = function(e) {
websocket_server.send(
JSON.stringify({
'type': 'socket',
'user_id': <?=$session?>,
})
);
};
websocket_server.onerror = function(e) {
// Errorhandling
}
websocket_server.onmessage = function(e) {
var json = JSON.parse(e.data);
switch(json.type) {
case 'chat':
$('#chat_output').append(json.msg);
break;
}
}
// Events
$('#chat_input').on('keyup',function(e){
if(e.keyCode==13 && !e.shiftKey) {
var chat_msg = $(this).val();
websocket_server.send(
JSON.stringify({
'type': 'chat',
'user_id': <?=$session?>,
'chat_msg': chat_msg
})
);
$(this).val('');
}
});
});
</script>
</div>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
