设为首页 加入收藏

TOP

WebSocket使用示例(一)
2015-11-10 13:46:05 来源: 作者: 【 】 浏览:21
Tags:WebSocket 使用 示例

HTML5给大家提供了一个很高大上的功能:WebSocket socket通信是在服务端与客户端之间进行的,所谓的WebSocket其实就是通过Html 与 后台服务器之间进行的消息传递;


下面我们来介绍一下如何实现一个简单的WebSocket实例。


注意:


JDK必须是1.7以上版本,tomcat必须是7以上版本;


spring对Websocket的支持也是只有Spring4之后才有


1、编写客户端程序


Html代码



?
?
?

?
?



?
?




?

?


js代码


var webSocket =new WebSocket(resourceDomain.replace("http://", "ws://") + 'coder_request?wx101id='+wx101id);
?
webSocket.onerror = function(event) {
onError(event)
};


webSocket.onopen = function(event) {
onOpen(event)
};


webSocket.onmessage = function(event) {
onMessage(event)
};
function onMessage(event) {
?var json =JSON.parse(event.data);
?var div = '

';
?if (json.creator) {
? div = div +'';
?}
?div = div + '
' + json.content+ '
';
?div = div + '
';
?$("#discussList").prepend(div);
}


function onOpen(event) {
?userWebsocket = true;
}


function onError(event) {
? ?alert(event.data);
}


2、编写服务端程序


package com.itbuilder.wx.web.socket;


import java.io.IOException;
import java.util.List;
import java.util.Map;


import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;


import com.itbuilder.framework.ApplicationContextHolder;
import com.itbuilder.framework.util.AssertUtil;
import com.itbuilder.framework.util.JSONUtil;
import com.itbuilder.wx.entity.Wxjc101;
import com.itbuilder.wx.entity.Wxjc102;
import com.itbuilder.wx.service.IWxjc102Service;
import com.itbuilder.wx.web.WebChatPool;



/**
?* 开发人员咨询socket
?* @author mrh
?*
?*/
@Controller
@ServerEndpoint("/coder_request")
public class CoderRequestSocket {
?
?private IWxjc102Service wx102Service;
?
?/**
? * LOGGER
? */
?private static final Logger LOGGER = Logger.getLogger(CoderRequestSocket.class);
?
?


?public CoderRequestSocket() {
? this.wx102Service = ApplicationContextHolder.getBean("wx102Service", IWxjc102Service.class);
?}


?/**
? * 接收到开发人员发送的消息
? * @param message
? * @param session
? * @throws IOException
? * @throws InterruptedException
? */
?@OnMessage
?public void onMessage(String content, Session session) {
? ? try {
? ? ?String toUserName = this.getToUserName(content);
? ? ?String message = this.getMessage(content);
? ? ?String wx101id = this.getPoolId(session);
? ? ?Wxjc102 wx102 = this.wx102Service.doSendMsg(message, toUserName, wx101id);
? ? ?Wxjc101 wx101 = this.wx102Service.queryWx101(wx101id);
? ? ?Session reciver = WebChatPool.getSession(wx101id, wx101.getModifier());
? ?session.getBasicRemote().sendText(JSONUtil.toString(wx102));
? ?if (AssertUtil.hasValue(reciver) && !reciver.getId().equals(session.getId())) {
? ? reciver.getBasicRemote().sendText(JSONUtil.toString(wx102));
? ?}
? ?reciver = WebChatPool.getSession(wx101id, toUserName);
? ?if (AssertUtil.hasValue(toUserName) && !reciver.getId().equals(session.getId())) {
? ? reciver.getBasicRemote().sendText(JSONUtil.toString(wx102));
? ?}
? } catch (IOException e) {
? ?LOGGER.error("接受消息失败 sessionId = " + session.getId(), e);
? } catch (Exception e) {
? ?LOGGER.error("接受消息失败 sessionId = " + session.getId(), e);
? }
?}
?
?/**
? * 获取发送人信息
? * @param content String
? * @return String
? */
?private String getMessage(String content) {
? if (!AssertUtil.hasValue(content)) {
? ?return null;
? }
? if (!content.contains("&")) {
? ?retu

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring+SpringMVC企业快速开发架.. 下一篇Python调用API接口的几种方式

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: