As business needs, the system in the background to add message push functionality, so go online access to relevant information, but information on the Internet are running in the front and rear end of a project, before and after the end of the separation now, so I toss myself a bit, in case to a lot of the pit.
Code is copied from the blog over the Internet, above all, the configuration file:
Front-end code, you need to import js three files:
<html><head><meta charset="UTF-8"><title>Hello topic</title><script src="./sock.js"></script><script src="./stomp.js"></script><script src="./jquery.min.js"></script><script type="text/javascript">var stompClient =null;functionsetConnected(connected){
document.getElementById("connect").disabled = connected;
document.getElementById("disconnect").disabled =!connected;$("#response").html();}functionconnect(){
console.log("111111111111");// 这里的端口需要写自己项目的端口var socket =newSockJS("http://localhost:7086/webSocketEndPoint");
stompClient = Stomp.over(socket);
stompClient.connect({},function(frame){setConnected(true);
console.log('Connected: '+ frame);// 这里的webSocket与后端的自定义字符串相匹配,用来接收后台发送的指向消息
stompClient.subscribe('/user/webSocket/msg',function(response){var response1 = document.getElementById('response');var p = document.createElement('p');
p.style.wordWrap ='break-word';
p.appendChild(document.createTextNode(response.body));
response1.appendChild(p);});});}functiondisconnect(){if(stompClient !=null){
stompClient.disconnect();}setConnected(false);
console.log("Disconnected");}functionsendName(){var name = document.getElementById('name').value;
console.info(1111111111);
stompClient.send("/subscribe",{},JSON.stringify({'name': name }));}</script></head><body onload="disconnect()"><noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being enabled. Please enable
Javascript and reload this page!</h2></noscript><div><div><button id="connect" onclick="connect();">Connect</button><button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button></div><div id="conversationDiv"><labal>名字</labal><input type="text" id="name"/><button id="sendName" onclick="sendName();">Send</button><p id="response"></p></div></div></body></html>
Code is ready, then start Springboot project, then use Nginx open Html page, I have not used Nginx Html open access, the front end will complain, as follows:
When sending a message, write their own test class, you can call the sendAndFormatToUsers method.
Then I want to test the maximum number of connections websocket, and went online to find some java code of connection websocket:
After testing, the maximum number of websocket connected to the current project can support 10,000, perfectly adequate project used, but through access to information to know, can increase the number of connections by configuring the maximum number of connections tomcat, when you can be 20,000 when the maximum number of connections configured the number of connections increased to 16,000.