万事开头难.工作需要研究websocket.公司之前都是用weblogic 11,所以搜索了weblogic+websocket的内容.找到了官方资料:
18 Using the WebSocket Protocol in WebLogic Server (还有一篇 17 Using WebSockets in WebLogic Server,这个应该是老版本.)
视频: youtube 教程: https://www.youtube.com/watch?v=BikL52HYaZg
在旧项目中添加了一个POJO类,增加了@ServerEndpoint("/chatroomServerEndpoint")注释以及对应的@OnMessage等方法.
但是客户端 var ws = new WebSocket("ws://127.0.0.1:7001/echo");一直提示404错误.
经过长久的google,终于意识到了问题.将web.xml文件头
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
修改为
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
问题解决.
注:
web.xml中添加如下片段,解决连接关闭问题: 默认30秒.
<context-param>
<param-name>weblogic.websocket.tyrus.session-max-idle-timeout</param-name>
<param-value>0</param-value>
</context-param>
附带吗:
@ServerEndpoint(value = "/chatroomServerEndpoint")
public class EchoEndpoint {
static Set<Session> all = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen(Session session) throws IOException {
all.add(session);
System.out.println("new session");
session.getBasicRemote().sendText("onOpen is invoked.");
}
public void onClose(Session session) {
all.remove(session);
System.out.println("session closed");
}
@OnMessage
public void echo(Session session, String message) throws IOException {
String username = (String) session.getUserProperties().get("username");
if (username == null) {
session.getUserProperties().put("username", message);
session.getBasicRemote().sendText(buildJsonData("System", "you are now connected as " + message));
} else {
Iterator<Session> iteartor = all.iterator();
while (iteartor.hasNext()) {
iteartor.next().getBasicRemote().sendText(buildJsonData(username, message));
}
}
}
private String buildJsonData(String username, String message) {
// TODO Auto-generated method stub
JsonObject jsonObject = Json.createObjectBuilder().add("message", username + ":" + message).build();
StringWriter stringWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {
jsonWriter.write(jsonObject);
}
return stringWriter.toString();
}
@OnError
public void onError(Throwable t) {
t.printStackTrace();
}
}
页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="UTF-8">
<script src="/websocket/static/js/jquery-1.8.1.min.js"
type="text/javascript"></script>
<script>
var ws = new WebSocket("ws://192.168.1.67:7001/chatroomServerEndpoint");//Right.
ws.onopen = function() {
console.log("open");
};
ws.onmessage = function(evt) {
var d = JSON.parse(evt.data);
if (d.message != null) {
$("#a").val($("#a").val() + d.message + "\n")
}
};
ws.onclose = function(evt) {
console.log("WebSocketClosed!");
};
ws.onerror = function(evt) {
console.log("WebSocketError!");
};
function send() {
ws.send(m.value);
};
</script>
<body>
<textarea id="a" readonly="readonly" rows="10" cols="50"></textarea>
<br />
<input type="text" id="m" size="50" />
<input type="button" value="Send" onClick="send();" />
</body>
</html>
运行结果见附图.