I'm trying to apply a connection with websockets, but when running, it gives the following error:
WebSocket connection to 'ws: //127.0.0.1: 8080 /' failed: Error in connection establishment: net :: ERR_CONNECTION_TIMED_OUT
I have tried the ports, 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);
?>