[英]How to specify URL for websocket connection handshake?
I'm trying to use websocket with php and js. 我正在尝试使用带有php和js的websocket。 I referred to many online resources to understand how the connection is established.
我提到了许多在线资源,以了解如何建立连接。 But I just can't understand how to specify the url.
但我只是无法理解如何指定网址。
I learnt from one of the resources I referref to, that URL is the server that I want to connect to. 我从我引用的其中一个资源中了解到,该URL是我想要连接的服务器。
My site structure now is this: 我的网站结构现在是这样的:
root/test/test.js
in the test.js
, my code to make connection is this: 在
test.js
,我的连接代码是这样的:
jQuery(document).ready(function($) {
WebSocketTest();
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://mysite.localhost.com");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert("Message is received...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
}
});
When I run the above code, it says.. 当我运行上面的代码时,它说..
1) websocket is supported by your browser 1)您的浏览器支持websocket
2) connection is closed... 2)连接关闭......
3) in the console, WebSocket connection to 'ws://mysite.localhost.com/' failed: Error during WebSocket handshake: Unexpected response code: 200
3)在控制台中,
WebSocket connection to 'ws://mysite.localhost.com/' failed: Error during WebSocket handshake: Unexpected response code: 200
Please explain to me only how to establish connection and to which file or server. 请向我解释如何建立连接以及文件或服务器。 Thanks in advance...
提前致谢...
here.. https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications 这里.. https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
this is the URL given for example.Can someone explain what that /socketserver means 这是给出的URL,例如。有人解释/ socketserver的含义
var exampleSocket = new WebSocket("ws://www.example.com/socketserver", "protocolOne");
Connection is made, if connected to ws://echo.websocket.org/
, but I'm not clear on this concept.. 如果连接到
ws://echo.websocket.org/
,则建立连接,但我不清楚这个概念..
Websocket requires you to create a endpoint on server side to etablish the connection. Websocket要求您在服务器端创建端点以建立连接。
But i can only see the js part of your code here, did you ever made a server endpoint for your websocket connection? 但我只能在这里看到你代码的js部分,你有没有为你的websocket连接建立一个服务器端点?
In "ws://www.example.com/socketserver" , the socketServer means the name of websocket server endpoint. 在“ws://www.example.com/socketserver”中,socketServer表示websocket服务器端点的名称。
So you need to do something in server side to make it work. 因此,您需要在服务器端执行某些操作才能使其正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.