How to close the old connection in WebSocket()?

It turns out that the Workerman created socket using websocket socket which monitors the incoming variables in the tcp server, and then processes it sends a 'ws://192.168.2.163:1000/?user='+id for a specific id. Now, there are buttons there may be any number, is there a tracking clicking while pressing the control button 1 - id changes to 1, pressing 2 - id changes to 2, etc. when every you open a connection at the new address ws://192.168.2.163:1000/?user=1 > ws://192.168.2.163:1000/?user=2, etc. on each connection receives the text, but there is trouble if 1 was opened after the discovery of compound 2, it receives the text from two compounds. Now, I can't figure out how with every new button with the id of the old connection was closed. Tried using ws.close(); but something do(
spoiler
var id = data['id'],
 url = 'ws://192.168.2.163:1000/?user='+id, 
 ws = new WebSocket(url);
 ws.addEventListener('message', (event) => {
console.log(event.data);
 });
March 23rd 20 at 19:09
1 answer
March 23rd 20 at 19:11
ws.close() should work. apparently do do something do.
Maybe we have to wait until really close. But the question is why you have the text right in both connections is sent - so there should not be even if you are an old socket do not close.
@Antonette_Greenholt, if not difficult, you can ask to look at these files and give the answer in what could be the error
socket.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
// create a ws-server. all your users will connect to it
$ws_worker = new Worker("websocket://192.168.2.163:1000");
$ws_worker->count = 4;// it will create 4 process
// storage of user-connection link
$users = [];
$ws_worker->onConnect = function($connection) use (&$users)
{
 $connection->onWebSocketConnect = function($connection) use (&$users)
{
 // put the get parameter in a collection $users the user
 // on the page you can set any parameter. for example client.html: ws = new WebSocket("ws://127.0.0.1:8000/?user=tester01");
 $users[$_GET['user']] = $connection;
 // or you can use another parameter for user identification, for example $_COOKIE['PHPSESSID']
};
};
$ws_worker->onClose = function($connection) use(&$users)
{
if(isset($users[$connection->uid]))
{
 // unset parameter when user is disconnected
unset($users[$connection->uid]);
}
};
// it will start once for each of the 4 ws-servers when you start server.php:
$ws_worker->onWorkerStart = function() use (&$users)
{
 //each ws-server connects to the local tcp server
 $connection = new AsyncTcpConnection("tcp://127.0.0.1:1005");
 $connection->onMessage = function($connection, $data) use (&$users) {
 // you have to use json_decode for $data because send.php uses json_encode
 $data = json_decode($data); // but you can use another protocol for send data send.php to local tcp server
 // send message to the user in the user Name
 if (isset($users[$data->user])) {
 $webconnection = $users[$data->user];
$webconnection->send($data->message);
}
};
$connection->connect();
};
// create a local tcp server. he will receive a message with your site code (e.g., from send.PHP)
$tcp_worker = new Worker("tcp://127.0.0.1:1005");
// create a handler that will be called when a tcp socket receives a message (e.g., from send.PHP)
$tcp_worker->onMessage = function($connection, $data) use ($tcp_worker)
{
 // forward the message to all other processes (you have 4 ws-servers)
 foreach ($tcp_worker->connections as $id => $webconnection) {
 if ($connection->id != $id) {
$webconnection->send($data);
}
}
};
// Run worker
Worker::runAll();
send.php
<?php
$localsocket = 'tcp://127.0.0.1:1005';
$user = $_POST['id'];
$message = $_POST['text'];

// connect to local tcp server
$instance = stream_socket_client($localsocket);
// send the message
fwrite($instance, json_encode([
 user => $user, 
 message => $message
]));
chat-manager.js
choiceNotific function(){
 $("a.notif-list").on("click", function(){
 var id = $(this).attr("id");
 // Output existing messages
$.ajax({
 type: 'POST',
 url: 'chat-manager.php',
data:{
 idNotific: id
 key: 2
},
success:function(response){
 data = JSON.parse(response);
$('#messages').html(data['m']);

 var idClient = data['idClient'],
 url = 'ws://192.168.2.163:1000/?user='+idClient, 
 ws = new WebSocket(url); // Connect to the server

 // Work with the Web socket 
 ws.addEventListener('message', (event) => {
console.log(event.data);
});
}
});
}
chat-client.js
function webSocket(){
 var text = $('#text-client').val(),
 id = getCookie('idClient');
$.ajax({
 type: 'POST',
 url: 'send.php',
data:{
 id: id,
 text: text
}
});
 }
- conor commented on March 23rd 20 at 19:14
I don't see where you close the socket on the client. In fact the code that you gave, you on each button to open a new connection, leaving the old work. If the server that will send both the socket will receive two messages.
PHP I forgot 10 years ago so it will not say anything. - Antonette_Greenholt commented on March 23rd 20 at 19:17

Find more questions by tags JavaScript