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://'+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:// > ws://, 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(
var id = data['id'],
 url = 'ws://'+id, 
 ws = new WebSocket(url);
 ws.addEventListener('message', (event) => {
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
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://");
$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://");
 $users[$_GET['user']] = $connection;
 // or you can use another parameter for user identification, for example $_COOKIE['PHPSESSID']
$ws_worker->onClose = function($connection) use(&$users)
 // unset parameter when user is disconnected
// 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://");
 $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];
// create a local tcp server. he will receive a message with your site code (e.g., from send.PHP)
$tcp_worker = new Worker("tcp://");
// 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) {
// Run worker
$localsocket = 'tcp://';
$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
choiceNotific function(){
 $("a.notif-list").on("click", function(){
 var id = $(this).attr("id");
 // Output existing messages
 type: 'POST',
 url: 'chat-manager.php',
 idNotific: id
 key: 2
 data = JSON.parse(response);

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

 // Work with the Web socket 
 ws.addEventListener('message', (event) => {
function webSocket(){
 var text = $('#text-client').val(),
 id = getCookie('idClient');
 type: 'POST',
 url: 'send.php',
 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