websocked

运行环境:

Tomcat7以上

Chrome
Supported in version 4+
Firefox
Supported in version 4+
Internet Explorer
Supported in version 10+
Opera
Supported in version 10+
Safari
Supported in version 5+


html代码:

<!DOCTYPE html>

<html>
  <head>
    <title>index.html</title>
    <meta charset=UTF-8> 
  </head>
  <body>
    <div>
<input type="hidden" value="Start" οnclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
var webSocket=null;
function connect() {
webSocket = new WebSocket('ws://10.0.0.77:8080/webSocket/websocket');
webSocket.onerror = function(event) {
onError(event);
};

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

webSocket.onmessage = function(event) {
onMessage(event);
};

webSocket.onclose = function(event) {
onClose(event);
};
}


function onMessage(event) {
document.getElementById('messages').innerHTML += '<br />' + event.data;
}


function onOpen(event) {
document.getElementById('messages').innerHTML = 'Connection established';
}

function onClose(event) {
document.getElementById('messages').innerHTML = 'Connection closed!';
webSocket.close();
}


function onError(event) {
alert(event.data);
alert("error");
}


function start() {
var value = document.getElementById('onfoc').value;
webSocket.send(value);
}

function focusfun(){
document.getElementById('onfoc').value="";
}
</script>
<input type="button" value="connect" οnclick="connect();" />
消息:<input type="text" id="onfoc" οnfοcus="focusfun();" value="请输入消息" />
<input type="button" οnclick="start();" value="发 送" />
<input type="button" οnclick="onClose();" value="close" />
  </body>

</html>


java 后台代码:


import java.io.IOException;
import java.util.ArrayList;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/websocket")
public class WebSocket {
@SuppressWarnings("rawtypes")
private static ArrayList mmiList = new ArrayList();  
@OnMessage
    public void onMessage(String message, Session session) 
    throws IOException, InterruptedException {

// Print the client message for testing purposes
System.out.println("Received: " + message);
System.out.println("mmiList.size() = " + mmiList.size());
send(message,session);
    }

@SuppressWarnings("unchecked")
@OnOpen
    public void onOpen (Session session) {
mmiList.add(session);
        System.out.println("登录人: = " + session.getId() + " Client connected");
        send("代号为 '"+session.getId() +"' 上线。",session);
    }


    @OnClose
    public void onClose (Session session) {
    mmiList.remove(session);
    System.out.println("退出人: = " + session.getId() + " Connection closed");
    send("代号为 '"+session.getId() +"' 下线。",session);
    }
    
    public void send(String message, Session session){
    Session se = null;
    for (int i = 0; i < mmiList.size(); i++) {
se = (Session)mmiList.get(i);
try {
se.getBasicRemote().sendText("代号为为' " + session.getId() + "' 的人发送消息 :" + message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值