0
I am trying to apply a connection with websockets, but when running, gives the following error:
Websocket Connection to 'Ws://127.0.0.1:8080/' failed: Error in Connection establishment: net:ERR_CONECTION_TIMED_OUT
I tried the doors, 80, 9000, 12345, etc., but the error persists.
var FancyWebSocket = function(url)
{
var callbacks = {};
var ws_url = url;
var conn;
this.bind = function(event_name, callback)
{
callbacks[event_name] = callbacks[event_name] || [];
callbacks[event_name].push(callback);
return this;
};
this.send = function(event_name, event_data)
{
this.conn.send( event_data );
return this;
};
this.connect = function()
{
if ( typeof(MozWebSocket) == 'function' )
this.conn = new MozWebSocket(url);
else
this.conn = new WebSocket(url);
this.conn.onmessage = function(evt)
{
dispatch('message', evt.data);
};
this.conn.onclose = function(){dispatch('close',null)}
this.conn.onopen = function(){dispatch('open',null)}
};
this.disconnect = function()
{
this.conn.close();
};
var dispatch = function(event_name, message)
{
if(message == null || message == "")
{
}
else
{
var JSONdata = JSON.parse(message);
switch(JSONdata[0].actualizacion)
{
case '1':
actualiza_mensaje(message);
break;
case '2':
actualiza_solicitud(message);
break;
}
}
}
};
var Server;
function send( text )
{
Server.send( 'message', text );
}
$(document).ready(function()
{
Server = new FancyWebSocket('ws://ip_do_servidor:80');
Server.bind('open', function()
{
});
Server.bind('close', function( data )
{
});
Server.bind('message', function( payload )
{
});
Server.connect();
});
function actualiza_mensaje(message)
{
var JSONdata = JSON.parse(message);
var tipo = JSONdata[0].tipo;
var mensaje = JSONdata[0].mensaje;
var fecha = JSONdata[0].fecha;
var contenidoDiv = $("#"+tipo).html();
var mensajehtml = fecha+' : '+mensaje;
$("#"+tipo).html(contenidoDiv+'0000111'+mensajehtml);
}
function actualiza_solicitud()
{
alert("tipo de envio 2");
}
Server
<?php
set_time_limit(0);
require 'classes/class.PHPWebSocket.php';
function wsOnMessage($clientID, $message, $messageLength, $binary)
{
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}
if ( sizeof($Server->wsClients) == 1 )
$Server->wsSend($clientID, "");
else
foreach ( $Server->wsClients as $id => $client )
$Server->wsSend($id,$message);
}
function wsOnOpen($clientID)
{
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "" );
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "");
}
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "" );
foreach ( $Server->wsClients as $id => $client )
$Server->wsSend($id, "");
}
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
$Server->wsStartServer('ip_do_servidor',80);
?>
You got some websocket service running through those doors?
– Miguel
Sorry Miguel. This is the first time I work with websockets. I don’t understand your question.
– user24136
Oh yes. If you have another service. I can’t tell you, because our server is remote.
– user24136
The error was timed out (required time exhausted). Miguel asked if the service is running on this door, or if it is just running. You can test using a telnet. Ex:
telnet 127.0.0.1 8080
, if closing the connection is because there is some service listening there..– BrTkCa
Fox I think you should start with something like this: https://www.youtube.com/watch?v=tHbCkikFfDE . What you have seems to me is just the client side, you have to have a websockets service running in this case in 127.0.0.1:8080.
– Miguel
Hello Lucas. Right. I’ll take a look.
– user24136
Hello Miguel. I updated my post with the server side. I contacted the server and they reported that the port is 80, but the error that gives in Chrome is:
WebSocket connection to 'ws://ip_do_servidor/' failed: Error during WebSocket handshake: Unexpected response code: 200
and in Firefox:O Firefox não conseguiu estabelecer uma conexão com o servidor ws://ip_do_servidor/.
– user24136
Fox,
$Server->wsStartServer('ip_do_servidor',80);
puts the correct ip. But it won’t be able to be 80 for sure, puts anything like 8080– Miguel
Hello Miguel. The ip was placed this way purposely, because it is the client’s server and I prefer to keep anonymous, combined?
– user24136