3
我可以从一个html页面打开两个到一个服务器的WebSocket连接,但两个不同的端口。HTML5从一个html页面打开两个网络套接字
我已经在谷歌搜索,但找不到任何有用的东西。你也可以给我链接到一些网络套接字教程。这是我发现的:
http://websocket.org/echo.html
http://www.html5rocks.com/en/tutorials/websockets/basics/
我可以从一个html页面打开两个到一个服务器的WebSocket连接,但两个不同的端口。HTML5从一个html页面打开两个网络套接字
我已经在谷歌搜索,但找不到任何有用的东西。你也可以给我链接到一些网络套接字教程。这是我发现的:
http://websocket.org/echo.html
http://www.html5rocks.com/en/tutorials/websockets/basics/
这会不会工作?
var socket1 = new WebSocket('ws://localhost:1001');
var socket2 = new WebSocket('ws://localhost:1002');
至于你的服务器如何处理它,这将在很大程度上取决于你的服务器端技术。
教程,的WebSockets的客户端方面是死很容易,这是相当多了:
var socket;
// Firefox uses a vendor prefix
if (typeof(MozWebSocket) != 'undefined') {
socket = new MozWebSocket('ws://localhost');
}
else if (typeof(WebSocket) != 'undefined') {
socket = new WebSocket('ws://localhost');
}
if (socket != null) {
socket.onmessage = function(event) {
// fired when a message is received from the server
alert(event.data);
};
socket.onclose = function() {
// fired when the socket gets closed
};
socket.onerror = function(event) {
// fired when there's been a socket error
};
socket.onopen = function() {
// fired when a socket connection is established with the server,
// Note: we can now send messages at this point
// sending a simple string to the server
socket.send('Hello world');
// sending a more complex object to the server
var command = {
action: 'Message',
time: new Date().toString(),
message: 'Hello world'
};
socket.send(JSON.stringify(command));
};
}
else {
alert('WebSockets are not supported by your browser');
}
如何处理传入的WebSocket连接服务器端的方面是很多更复杂,取决于您的服务器端技术(ASP.NET,PHP,Node.js等)。
我一直在试图实现WebSockets,但问题是我们的生产服务器是相当安全的,即使安装像node.js或socket.io服务是否有一个纯粹的PHP5实现或只是JavaScript,我们可以上传项目到网络服务器和一切正常,没有更多的安装或配置要完成。可能吗?我们使用cakePHP 2.3作为我们的php框架 – indago