Weblogic 12.1.3+websocket

本文介绍了如何解决在WebLogic服务器上使用WebSocket遇到的404错误问题,通过更新web.xml文件版本并配置WebSocket参数,成功实现了WebSocket服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

万事开头难.工作需要研究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

资料:Jdon java EE 7教程

在旧项目中添加了一个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>

运行结果见附图.


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值