当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net Core中WebSocket绑定的方法详解

Asp.Net Core中WebSocket绑定的方法详解

2018年01月25日  | 移动技术网IT编程  | 我要评论

浪莎迪士尼冰雪袜,任文国,云散高唐txt

说明

websocket是html5后的产物,对于asp.net core中也得到了支持,asp.net core中webscoket的操作使用基本上和asp.net中相同,不同的是,绑定监听。

asp.net core2.0默认已经支持websocket,不需要另外安装nuget包。

通过对httpcontext中的websockets.acceptwebsocketasync方法,接受websocket请求;并返回webscoket对象。

下面话不多说了,来一起看看详细的介绍吧。

一、示例1,

1.后台启动文件startup的configure中绑定websocket的路由监听

public void configure(iapplicationbuilder app, ihostingenvironment env, iserviceprovider svp) 
{ 
...... 
 //绑定websocket 
 app.map("/wsone/connect", (con) => 
 { 
  con.usewebsockets(); 
  wshanletwo _two = new wshanletwo(); 
  con.use(_two.connect); 
 }); 
} 

2.定义请求处理类

using system.net.websockets; 
using system.threading; 
using system.threading.tasks; 
namespace core_razor_2 
{ 
 public class wshanletwo 
 { 
  private websocket socket = null; 
  //创建链接 
  public async task connect(httpcontext context, func<task> n) 
  { 
   try 
   { 
    //执行接收 
    websocket socket = await context.websockets.acceptwebsocketasync(); 
    this.socket = socket; 
    //执行监听 
    await echoloop(); 
   } 
   catch (exception ex) 
   { 
    throw ex; 
   } 
  } 
  /// <summary> 
  /// 响应处理 
  /// </summary> 
  /// <returns></returns> 
  async task echoloop() 
  { 
   var buffer = new byte[1024]; 
   var seg = new arraysegment<byte>(buffer); 
   while (this.socket.state == websocketstate.open) 
   { 
    var incoming = await this.socket.receiveasync(seg, cancellationtoken.none); 
    byte[] backinfo = system.text.utf8encoding.default.getbytes("服务端相应内容"); 
    var outgoing = new arraysegment<byte>(backinfo, 0, incoming.count); 
    await this.socket.sendasync(outgoing, websocketmessagetype.text, true, cancellationtoken.none); 
   } 
  } 
 } 
} 

3.前台请求代码

var socket; 
//var uri = "ws://" + window.location.host + "/ws"; 
var uri = "ws://" + window.location.host + "@url.action("connect")"; 
var output; 
var text = "test echo"; 
function write(s) { 
 var p = document.createelement("p"); 
 p.innerhtml = s; 
 output.appendchild(p); 
} 
function doconnect() { 
 socket = new websocket(uri); 
 socket.onopen = function (e) { write("opened " + uri); dosend(); }; 
 socket.onclose = function (e) { write("closed"); }; 
 socket.onmessage = function (e) { write("received: " + e.data); socket.close(); }; 
 socket.onerror = function (e) { write("error: " + e.data); }; 
} 
function dosend() { 
 write("sending: " + text); 
 socket.send(text); 
} 
function oninit() { 
 output = document.getelementbyid("output"); 
 doconnect(); 
} 
window.onload = oninit; 

二、为了简单绑定,可以这样封装

public class sockethandler 
{ 
 public const int buffersize = 4096; 
 websocket socket; 
 sockethandler(websocket socket) 
 { 
  this.socket = socket; 
 } 
 async task echoloop() 
 { 
  var buffer = new byte[buffersize]; 
  var seg = new arraysegment<byte>(buffer); 
  while (this.socket.state == websocketstate.open) 
  { 
   var incoming = await this.socket.receiveasync(seg, cancellationtoken.none); 
   var outgoing = new arraysegment<byte>(buffer, 0, incoming.count); 
   await this.socket.sendasync(outgoing, websocketmessagetype.text, true, cancellationtoken.none); 
  } 
 } 
 static async task acceptor(httpcontext hc, func<task> n) 
 { 
  if (!hc.websockets.iswebsocketrequest) 
   return; 
  var socket = await hc.websockets.acceptwebsocketasync(); 
  var h = new sockethandler(socket); 
  await h.echoloop(); 
 } 
 /// <summary> 
 /// 路由绑定处理 
 /// </summary> 
 /// <param name="app"></param> 
 public static void map(iapplicationbuilder app) 
 { 
  app.usewebsockets(); 
  app.use(sockethandler.acceptor); 
 } 
} 

路由绑定:

[csharp] view plain copy
//绑定websocket 
app.map("/ws", sockethandler.map); 

asp.net core上传控件:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网