= function (){Console. log ("handshake successful ");};Ws. onerror = function (){Console. log ("error ");};
Run the server code. when the client is connected, we can see:
2. Extract Sec-WebSocket-Key information
The Code is as follows:
Function getKey ($ req ){$ Key = null;If (preg_match ("/Sec-WebSocket-Key: (. *) \ r \ n/", $ req, $ match )){$ Key = $ match [1];}Return $ key;}
This is relatively
a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.
Host A's application communicates with host B's application and must establish a connection through the socket, and the socket connection must be built with the underlying TCP
. Java Implementation Method
There are several ways to use WebSocket at the Spring end1. How to use Java EE72. Using the interface provided by Spring3. Using the STOMP protocol and Spring MVC
The third way is to see the official documentation for Spring, based on WebSocket, using the simple Text oriented message Protocol (STOMP) protocol: Using
1.WebSocket Introduction1.1 ConceptsWebSocket is a series of new APIs in HTML5, or new specifications, new technologies. The support page uses the Web socket protocol for full-duplex communication with the remote host. It introduces the WebSocket interface and defines a full-duplex communication channel that operates on the web through a single socket.1.2 WebSocket
above support JSR-356, specific documents can be viewedIdeasWhen a connection is established, a thread is opened to poll the data in the database, and if the query changes to the data, a message is sent to the WebSocket implementation class, and the implementation class receives the message and pushes the message to the connected user.(If the data is added through the background, you don't have to be so troublesome, just send the message to the
In this article we first understand the basic knowledge, so that we can learn more useful behind.Socket,websocket,http,tcp and so on we have heard the ear has a cocoon, but the use of time to review it.University Learning Network Foundation when the teacher said, the network from the bottom up into the physical layer, Data link layer, network layer, transport layer, Session layer, presentation layer and application layer. Through the preliminary under
.
Vi. Summary
The project class uses WebSocket to realize a simple web chat room, in fact, websocket not only can be applied to the browser, but also can be applied to desktop clients.
Seven, study questions
This project class is simply the realization of the chat room, in fact, there are many functions can be increased, such as Avatar upload function, one-on
transport layer's TCP, there are more problems. I believe everyone who has created the wheel will feel it.
Even if HTTP is used, problems such as performance, network latency, and reverse notification (the server actively notifies the client) occur.
Therefore, WebSocket has emerged to solve this series of problems within a certain range.
Use
Directly go to the Code to show how to use WebSocket
C ++ code (s
");
};
Ws.onerror = function () {
Console.log ("error");
};
Running the server code, when the client connects, we can see:
2. Extract Sec-websocket-key Information
Copy Code code as follows:
function Getkey ($req) {
$key = null;
if (Preg_match ("/sec-websocket-key: (. *) \r\n/", $req, $match)) {
$key = $match [1];
}
return $key;
}
Here is relatively
[This article is original, if reproduced, please indicate the source http://blog.csdn.net/yl02520/article/]
Browser has supported the HTTP protocol, so why develop a new websocket protocol? We know that the HTTP protocol is a one-way network protocol, after the connection is established, it only allows Browser/ua (useragent) to send webserver request resources, webserver can return the appropriate data. And webserver can not take the initiative to pus
Recent projects in the use of Nginx Push_stream_module push module to implement server-to-client information push, originally only want to achieve this one-way communication needs, but to the client push the message, if you want to let the client give a feedback, There's no way to listen to the feedback event that gets to the client and respond in a timely manner. Later know that Push_stream_module management long connection is based on Pub/sub mode, and as if the module does not give a similar
http://blog.csdn.net/shangmingtao/article/details/75810099
Directory:
Catalog WebSocket Introduction Project Background hardware environment and client support this article is based on the Javaxwebsocket service-side code source to follow up the GIT Connection client code problem Exploration August 3 supplemental Intermediate line disconnection situation how to support thousands of client simultaneous online people Follow-up August 3 supplementary te
=OnClose; Socket.onerror=OnError; Socket.onmessage=OnMessage; Script> Body>HTML>Entirecode
Third, node create server
Create a simple server with node to see the entire websocket process.Because it is the WebSocket protocol, you need to introduce a WS module in node, assuming we are listening on a port number of 8080.As follows://
, and I have to sigh here. Because it is a small agile team, version control is required. In terms of jar package control, you still want to use maven to control it. You can directly search for the maven central database. There are still some. After the group discussion, I decided to use tomcat7.039 (it seems that 40 has come out), which solved the issue of version control and jar package configuration. Pom:
The next step is to solve the architecture problem. Currently, there are few p
What is WebSocket?
WebSocket is a persistent protocol, which is relative to HTTP non-persistence. to give a simple example, The life cycle of http1.0 is defined by the request, that is, a request, a response, for HTTP, this time the client and server session to this end, and in http1.1, slightly improved, that is, the addition of keep-alive, that is, in a Multipl
First, WebSocket simple introduction:When it comes to web real-time push, you have to say websocket. Before the advent of websocket, very many sites were implemented in order to implement real-time push technology. Commonly used scenariosis polling (Polling) and comet technology, comet can be subdivided into two ways,
) {$key = null;if (Preg_match ("/sec-websocket-key: (. *) \r\n/", $req, $match)) {$key = $match [1];}return $key;}
Here is relatively simple, direct regular matching, websocket information header must contain Sec-websocket-key, so we match up also relatively fast ~
3. Encrypt Sec-w
= require (' Express ') (); var http = require (' http '). Server (APP), var io = require (' Socket.io ') (HTTP), App.get ('/', function (req, res) {res.send (' Command line run node Index.js, if all goes well, you should see the listening on *:3000 returned, which indicates the service has been successfully built. Now open in Browserhttp://localhost:3000 should be able to see the normal welcome page.If you want to let the service run online server, and can be accessed through the domain name,
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.