Java's most comprehensive entry-level mastery notes for maven, git, spring, springMVC, mybatis (xmind (download required))

Java is the most comprehensive in the whole network of maven, git, spring, springMVC, mybatis introductory mastery notes (xmind (download required)).

The most complete, no one, does not support rebuttal

File: n459.com/file/25127180-479205530

The following are irrelevant:

-------------------------------------------Dividing line----- ----------------------------------------

For online customer service and marketing systems, the visitor side refers to Internet users who browse the website, or users who communicate with back-end customer service through embedded chat pages such as APP and WeChat. In this article, I will introduce in detail how to use .net core In the environment, WebSocket technology is used to realize the communication between the visitor and the server on the web page.

There are several technical difficulties that need attention:

The chat interface should be able to be seamlessly embedded in the customer's target website, without any influence on the original website.
Visitors can browse the website while chatting through the floating box in the lower right corner of the website, and the chat cannot be interrupted by web page jumps or star swiping.
It is necessary to consider the unstable connection of the chat page on the mobile phone, and to be able to maintain the chat status and information when the APP or browser switches to the background of the mobile phone and loses the connection.
The effect achieved by the visitor:

The effect of the visitor on the phone:

Realization effect of back-end customer service:

Configure middleware in asp.net core
First of all, we need to enable WebSocket middleware in Startup.cs:

var webSocketOptions = new WebSocketOptions()
{
KeepAliveInterval = TimeSpan.FromSeconds(120),
};

app.UseWebSockets(webSocketOptions); The
following settings can be configured:

KeepAliveInterval-How often to send "ping" frames to the client to ensure that the agent keeps the connection open. The default value is 2 minutes.
ReceiveBufferSize-The size of the buffer used to receive data. Advanced users may need to change it to adjust performance based on data size. The default value is 4 KB.
AllowedOrigins-The list of allowed Origin header values ​​used for WebSocket requests. By default, all sources are allowed. For details, see "WebSocket Source Restrictions" below.
Receiving the request from the visitor At the end of
the request life cycle (for example, in the later period of the Configure method or operation method), check whether it is a WebSocket request and accept the WebSocket request.

app.Use(async (context, next) =>
{
if (context.Request.Path == “/ws”)
{
if (context.WebSockets.IsWebSocketRequest)
{
using (WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync())
{
await Echo(context, webSocket);
}
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}

});
Perform await on Task during the request, as shown in the following example:

app.Use(async (context, next) =>
{
using (WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync())
{
var socketFinishedTcs = new TaskCompletionSource();

    BackgroundSocketProcessor.AddSocket(webSocket, socketFinishedTcs);

    await socketFinishedTcs.Task;
}

});
If the return from the operation method is too fast, a WebSocket close exception may also occur. When accepting the socket in the operation method, you need to complete the operation with the code of the socket, and then return from the operation method.

The
AcceptWebSocketAsync method of sending and receiving guest messages upgrades the TCP connection to a WebSocket connection and provides a WebSocket object. Use WebSocket objects to send and receive messages.
The code shown earlier that accepts WebSocket requests passes the WebSocket object to the Echo method. The code receives the message and immediately sends back the same message. Send and receive messages in a loop until the client closes the connection:

private async Task Echo(HttpContext context, WebSocket webSocket)
{
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
await webSocket.SendAsync(new ArraySegment(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);

    result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);

}
If the WebSocket connection is accepted before the loop starts, the middleware pipeline will end. After closing the socket, the pipe is expanded. That is, when accepting WebSocket, the request stops advancing in the pipeline. When the loop ends and the socket is closed, the request continues back to the pipe.

Dealing with the disconnection on the
guest side When the guest side is disconnected due to loss of connection, it will not automatically send a notification to the server. The server will only receive the disconnect message when the client sends a notification.
If the client does not always send messages and does not want to set a timeout just because the connection becomes idle, let the client use a timer and send a heartbeat message every few seconds. On the server, if a message has not arrived within seconds after the previous message was sent, the connection is terminated and the client is reported to have disconnected.

This article briefly introduces the use of WebSocket to build a guest communication framework. In the next article, I will specifically deconstruct the structure and design of the server program and the structure and design of the customer service program, so stay tuned.

Guess you like

Origin blog.csdn.net/gumenghua_com1/article/details/112650023