問題:我想通過tomcat發布一個websocket程序,代碼都已經調通。但是,前端並發最多只能支持260websocket連接就上不去了,以下是我的前端測試代碼和tomcat的配置。請大家幫我看看是我寫錯了麼,還是說單個tomcat的瓶頸就是如此?
前端代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src=" <%=request.getContextPath() %>/js/jquery/jquery-1.11.3.js"></script>
<script src=" <%=request.getContextPath() %>/js/others/sockjs-0.3.min.js"></script>
<script src=" <%=request.getContextPath() %>/js/others/stomp-2.3.3.js"></script>
</head>
<body>
<input type="button" value="連接" onclick="opens()" />
<input type="button" value="斷開" onclick="closes()" />
<select id="type">
<option value=1>點對點</option>
<option value=2>訂閱</option>
<option value=3>主題訂閱</option>
</select>
<input type="button" value="發送" onclick="send()"/>
用戶名:<input type="text" id="name" value="bulbuls"/>
<hr/>
<table>
<tr>
<td>消息</td>
<td><input type="text" id="msg"/></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td><td></td>
</tr>
<tr>
<td>message</td>
<td><div id="message"></div></td>
<td>返回的消息</td>
<td><div id="recode"></div></td>
</tr>
</table>
<hr/>
<script>
var i = 0;
function opens(){
i++;
if(i>300){
return;
}
var socket = new SockJS("http://127.0.0.1:8080/hello");
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
message("連接成功....:"+i);
//全域廣播
stompClient.subscribe('/topic', function(greeting){
$("#recode").append("<div>"+greeting.body+"</div>");
});
opens();
//主題廣播
/* stompClient.subscribe('/topic/'+$("#name").val(), function(greeting){
$("#recode").append("<div>"+greeting.body+"</div>");
}); */
//點對點方式回調
/* stompClient.subscribe('/user/'+$("#name").val()+'/point', function(message){
$("#recode").append("<div>"+message.body+"</div>");
}); */
});
}
function closes(){
if (stompClient != null) {
stompClient.disconnect();
message("斷開連接....")
}
}
function send(){
if(!stompClient || !stompClient.connected){
message("警告:連接已經斷開");
return;
}
var msg = $("#msg").val();
var name = $("#name").val();
var type = $("#type").val();
if(type==1){
stompClient.send("/app/point", {}, JSON.stringify({"user":"bulbuls","destination":"/point","message":"來自手機的消息"}));
}else if(type==2){
stompClient.send("/app/topic", {}, JSON.stringify({"user":"bulbuls","destination":"/topic","message":"來自手機的消息"}));
}else if(type==3){
stompClient.send("/app/topic/bulbuls", {}, JSON.stringify({"user":"bulbuls","destination":"/point","message":"來自手機的消息"}));
}
}
function message(mess){
$("#message").append("<div>"+mess+"</div>");
}
</script>
</body>
</html>
tomcat server.xml配置
<Service name="Catalina">
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="400" minSpareThreads="4"/>
<Connector connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" maxThreads="300"/>
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="C:\soft\apache-tomcat-7.0.73\wtpwebapps\websocket" path="/" reloadable="true" source="org.eclipse.jst.j2ee.server:websocket"/></Host>
</Engine>
</Service>
已經解決,原來除了tomcat的連接數限制以外, 浏覽器本身對websocket的連接數也有限制,谷歌261左右 IE:16個左右