谁能提供一个html5消息推送的实例,服务端为asp.net?谢谢

2016 年 7 月 13 日6060

用户提出问题:谁能提供一个html5消息推送的实例,服务端为asp.net?谢谢,具体如下:

通过互联网整理获得以下解决方法:

=================1楼=====================

该回复于2014-04-05 17:27:18被管理员删除

=================2楼=====================

不太明白你要的功能,不过你可以看下WebWorkers是不是你需要的

=================3楼=====================

websocket,你可以看一看的哦

=================4楼=====================

html5利用websocket完成的推送功能

链接:http://http://www.zjjv.com///web/a/2014032492868.shtml

希望能帮到你!

=================5楼=====================

程序代码

html部分

<!DOCTYPEHTML>

<html>

<head>

<metacharset="utf-8">

<scripttype="text/javascript"src="js/jquery.js"></script>

<scripttype="text/javascript"src="js/socket.js"></script>

<title>无标题文档</title>

</head>

<scriptlanguage="javascript">

</script>

<body>

<table>

<tr>

<td>Message</td>

<td><inputtype="text"id="message"></td>

</tr>

<tr>

<td>Name</td>

<td><inputtype="text"id="othername"></td>

</tr>

<tr>

<td><inputid="sendbutton"type="button"value="send"onClick="click"disabled="true">

</input></td>

</tr>

</table>

<script>

</script>

</body>

</html>

js部分(关于jquery部分不进行讲解)

varusername=window.prompt("输入你的名字:");

document.write("Welcome<pid=\"username\">"+username+"</p>");

if(!window.WebSocket&&window.MozWebSocket)

window.WebSocket=window.MozWebSocket;

if(!window.WebSocket)

alert("NoSupport");

varws;

$(document).ready(function(){

$("#sendbutton").attr("disabled",false);

$("#sendbutton").click(sendMessage);

startWebSocket();

})

functionsendMessage()

{

varothername=$("#othername").val();

varmsg="MSG\t"+username+"_"+othername+"_"+$("#message").val();

send(msg);

}

functionsend(data)

{

console.log("Send:"+data);

ws.send(data);

}

functionstartWebSocket()

{

ws=newWebSocket("ws://"+location.host+"/WebSocket/SocketServer");

ws.onopen=function(){

console.log("successopen");

$("#sendbutton").attr("disabled",false);

};

ws.onmessage=function(event)

{

console.log("RECEIVE:"+event.data);

handleData(event.data);

};

ws.onclose=function(event){

console.log("Clientnotifiedsockethasclosed",event);

};

}

functionhandleData(data)

{

varvals=data.split("\t");

varmsgType=vals[0];

switch(msgType)

{

case"NAME":

varmsg=vals[1];

varmes="NAME"+"\t"+msg+"_"+username;

send(mes);

break;

case"MSG":

varval2s=vals[1].split("_");

varfrom=val2s[0];

varmessage=val2s[2];

alert(from+":"+message);

break;

default:

break;

}

}

=================6楼=====================

java部分

importjava.io.IOException;

importjava.nio.ByteBuffer;

importjava.nio.CharBuffer;

importjavax.servlet.http.HttpServletRequest;

importjava.util.Set;

importjava.util.concurrent.CopyOnWriteArraySet;

importorg.apache.catalina.websocket.MessageInbound;

importorg.apache.catalina.websocket.StreamInbound;

importorg.apache.catalina.websocket.WsOutbound;

importorg.apache.catalina.websocket.WebSocketServlet;

publicclassSocketServerextendsWebSocketServlet{

privatestaticfinallongserialVersionUID=1L;

publicfinalSet<ChatWebSocket>users=newCopyOnWriteArraySet<ChatWebSocket>();

publicstaticintUSERNUMBER=1;

@Override

protectedStreamInboundcreateWebSocketInbound(Stringarg0,

HttpServletRequestarg1){

//TODOAuto-generatedmethodstub

returnnewChatWebSocket(users);

}

publicclassChatWebSocketextendsMessageInbound{

privateStringusername;

privateSet<ChatWebSocket>users=newCopyOnWriteArraySet<ChatWebSocket>();;

publicChatWebSocket(){

}

publicChatWebSocket(Set<ChatWebSocket>users){

this.users=users;

}

@Override

protectedvoidonTextMessage(CharBuffermessage)throwsIOException{

//这里处理的是文本数据

}

publicvoidonMessage(Stringdata){

String[]val1=data.split("\\t");

if(val1[0].equals("NAME"))

{

String[]val2=val1[1].split("_");

for(ChatWebSocketuser:users){

if(user.username.equals(val2[0])){

user.username=val2[1];

}

}

}

elseif(val1[0].equals("MSG"))

{

String[]val2=val1[1].split("_");

for(ChatWebSocketuser:users){

if(user.username.equals(val2[1])){

try{

CharBuffertemp=CharBuffer.wrap(data);

user.getWsOutbound().writeTextMessage(temp);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

}

else

{

System.out.println("ERROR");

}

}

@Override

protectedvoidonOpen(WsOutboundoutbound){

//this.connection=connection;

this.username="#"+String.valueOf(USERNUMBER);

USERNUMBER++;

try{

Stringmessage="NAME"+"\t"+this.username;

CharBufferbuffer=CharBuffer.wrap(message);

this.getWsOutbound().writeTextMessage(buffer);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

users.add(this);

}

@Override

protectedvoidonClose(intstatus){

users.remove(this);

}

@Override

protectedvoidonBinaryMessage(ByteBufferarg0)throwsIOException{

}

}

}

=================7楼=====================

定时刷新也行,省资源

=================8楼=====================

该回复于2016-05-25 16:48:10被管理员删除

=================9楼=====================

asp,你多搜搜,asp动态语言部分?接触不多

=================10楼=====================

SignalR

http://http://www.zjjv.com///translate/websocket-libraries-comparison-2

如果您还有更好的解决方法,请在最下面评论中留下您的解决方法

(转载请注明出处和本文链接)

+1

0人

0 0