duanbei3747 2013-11-20 04:31
浏览 382
已采纳

使用websockets通过localhost连接remotehost

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);
  • 写回答

2条回答 默认 最新

  • drf16571 2014-01-04 23:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题