underscore
underscore

Reputation: 6887

connect remotehost through localhost using websockets

I'm using this web socket hosted by google

var host = "ws://localhost:12345/websocket/server.php";
try{
  socket = new WebSocket(host);
  log('WebSocket - status '+socket.readyState);
  socket.onopen    = function(msg){ log("Welcome - status "+this.readyState); };
  socket.onmessage = function(msg){ log("Received: "+msg.data); };
  socket.onclose   = function(msg){ log("Disconnected - status "+this.readyState); };
}
catch(ex){ log(ex); }

is this work with remotehost ?

var host = "ws://example.com:12345/websocket/server.php";

I want to POST changes using CURL using websockets.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://xxxx.com/ccc.php");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CAINFO, "esoftcareers.crt");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 
http_build_query(array('PHPSESSID' => $id ,'query' => $resultQuery,'operator' => $operator,'database' => DATABASE)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

Upvotes: 3

Views: 1852

Answers (2)

Alex
Alex

Reputation: 11255

First of all let start my answer from this:

I want to POST changes using CURL using websockets.

WebSocket is protocol first of all for browser-based(not server to server) applications that need two-way communication with servers. You want to use use http/https protocols in curl(because post is the method of HTTP protocol) which are the layer over TCP. The WebSocket is also layered over TCP. So you want to use one TCP layer by another. Now you can't to do it, because php curl don't support websocket protocol. And I think it still immutable in the future.

Also post is method of HTTP protocol which has body and has one response. WebSocket is protocol without body and it use a sequence of frames. So there is no anything like post method in websocket protocol and this is his feature.

And you can't send something server and forgot about it in websocket, because socket means communicate. You must communicate with server when using websocket. In HTTP via AJAX you can send something to server and use async callbacks to process the response. In websocket if you close it you will lose all your data in response.

You can emulate websocket by curl using long-pulling requests. But it's still HTTP protocol, not websocket. So now curl can't use websocket protocol.

The second. About websocket protocol origin(via cross domain). Yes, websocket can use cross domain, because it use origin-based security model. So if you try to open connection with web server by WebSocket from domain which is absent in Origin request header server will return appropriate HTTP error. Usually 403 HTTP error by default. In other case all must be fine.

Upvotes: 1

Siva Tumma
Siva Tumma

Reputation: 1701

Server and client both should be aware of the websocket implementation and the first handshake should conclude that they both do know. by the first next line from the ws: response from the server ... "Upgrade: WebSocket\r\n". If there is no such response from server, the websocket expecting client should understand that there is no support for websocket in the currently contacting server and fallback to normal communication.

ADDED NOTE : Plain curl things could not benefit from this protocol unless you have an advance curl command which is updated with this protocol implementation. Please find this and this.

Upvotes: 0

Related Questions