Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer #379

Merged
merged 8 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(Senparc.Weixin.MP.Sample.App_Start.Startup))]

namespace Senparc.Weixin.MP.Sample.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.WebSockets;

namespace Senparc.Weixin.MP.Sample.Controllers
{
public class WxWebSocketController : ApiController
{
HttpContextBase context;
public WxWebSocketController()
{
context = new HttpContextWrapper(HttpContext.Current);
}

[HttpGet]
public string App()
{
//Checks if the query is WebSocket request.
if (context.IsWebSocketRequest)
{
//If yes, we attach the asynchronous handler.
context.AcceptWebSocketRequest(WebSocketRequestHandler);
}
return "";
}

public async Task WebSocketRequestHandler(AspNetWebSocketContext webSocketContext)
{
//Gets the current WebSocket object.
WebSocket webSocket = webSocketContext.WebSocket;

/*We define a certain constant which will represent
size of received data. It is established by us and
we can set any value. We know that in this case the size of the sent
data is very small.
*/
const int maxMessageSize = 1024;

//Buffer for received bits.
var receivedDataBuffer = new ArraySegment<Byte>(new Byte[maxMessageSize]);

var cancellationToken = new CancellationToken();

//Checks WebSocket state.
while (webSocket.State == WebSocketState.Open)
{
//Reads data.
WebSocketReceiveResult webSocketReceiveResult =
await webSocket.ReceiveAsync(receivedDataBuffer, cancellationToken);

//If input frame is cancelation frame, send close command.
if (webSocketReceiveResult.MessageType == WebSocketMessageType.Close)
{
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure,
String.Empty, cancellationToken);
}
else
{
byte[] payloadData = receivedDataBuffer.Array.Where(b => b != 0).ToArray();

//Because we know that is a string, we convert it.
string receiveString =
System.Text.Encoding.UTF8.GetString(payloadData, 0, payloadData.Length);

//Converts string to byte array.
var newString =
String.Format("Hello, " + receiveString + " ! Time {0}", DateTime.Now.ToString());
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(newString);

//Sends data back.
await webSocket.SendAsync(new ArraySegment<byte>(bytes),
WebSocketMessageType.Text, true, cancellationToken);
}
}
}

// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/<controller>/5
public string Get(int id)
{
return "value";
}

// POST api/<controller>
public void Post([FromBody]string value)
{
}

// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}

// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
<HintPath>..\packages\elmah.corelibrary.1.2.2\lib\Elmah.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -66,6 +74,7 @@
<Reference Include="System.Web.Abstractions" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Routing" />
<Reference Include="System.Web.Services" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="EntityFramework">
Expand Down Expand Up @@ -145,6 +154,7 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="App_Start\Startup.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\AsyncMethodsController.cs" />
<Compile Include="Controllers\BaseController.cs" />
Expand All @@ -169,11 +179,15 @@
<Compile Include="Controllers\WeixinController_OldPost.cs" />
<Compile Include="Controllers\TenPayController.cs" />
<Compile Include="Controllers\WxOpen\WxOpenController.cs" />
<Compile Include="Controllers\WxOpen\WxWebSocketController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\ProductModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WxWebSocket.ashx.cs">
<DependentUpon>WxWebSocket.ashx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\Document\Config.xml" />
Expand Down Expand Up @@ -321,6 +335,7 @@
<Content Include="Views\Cache\Test.cshtml" />
<Content Include="Views\Document\Index.cshtml" />
<Content Include="Views\AsyncMethods\Index.cshtml" />
<Content Include="WxWebSocket.ashx" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\Document\Files\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
<!-- 企业号 -->
<add key="WeixinCorpId" value="WeixinCorpId"/>
<add key="WeixinCorpSecret" value="WeixinCorpSecret"/>

<!-- 小程序 -->
<!-- 小程序消息URL对接信息 -->
<add key="WxOpenToken" value="小程序消息URL对应的Token"/>
<add key="WxOpenEncodingAESKey" value="小程序消息URL对应的消息加解密密钥"/>
<!-- 小程序秘钥信息 -->
<add key="WxOpenAppId" value="微信小程序AppId"/>
<add key="WxOpenAppSecret" value="微信小程序AppSecret"/>

<!-- Cache.Redis连接配置 -->
<add key="Cache_Redis_Configuration" value="Redis配置"/>
<!--<add key="Cache_Redis_Configuration" value="localhost:6379"/>-->
Expand Down Expand Up @@ -160,6 +160,10 @@
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ WebHandler Language="C#" CodeBehind="WxWebSocket.ashx.cs" Class="Senparc.Weixin.MP.Sample.WxWebSocket" %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.WebSockets;
using Senparc.Weixin.Helpers;

namespace Senparc.Weixin.MP.Sample
{
/// <summary>
/// WxWebSocket 的摘要说明
/// </summary>
public class WxWebSocket : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");

//Checks if the query is WebSocket request.
if (context.IsWebSocketRequest)
{
//If yes, we attach the asynchronous handler.
context.AcceptWebSocketRequest(WebSocketRequestHandler);
}
}

public async Task WebSocketRequestHandler(AspNetWebSocketContext webSocketContext)
{
//Gets the current WebSocket object.
WebSocket webSocket = webSocketContext.WebSocket;

/*We define a certain constant which will represent
size of received data. It is established by us and
we can set any value. We know that in this case the size of the sent
data is very small.
*/
const int maxMessageSize = 1024;

//Buffer for received bits.
var receivedDataBuffer = new ArraySegment<Byte>(new Byte[maxMessageSize]);

var cancellationToken = new CancellationToken();

//Checks WebSocket state.
while (webSocket.State == WebSocketState.Open)
{
//Reads data.
WebSocketReceiveResult webSocketReceiveResult =
await webSocket.ReceiveAsync(receivedDataBuffer, cancellationToken);

//If input frame is cancelation frame, send close command.
if (webSocketReceiveResult.MessageType == WebSocketMessageType.Close)
{
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure,
String.Empty, cancellationToken);
}
else
{
byte[] payloadData = receivedDataBuffer.Array
.Where(b => b != 0)
.Take(webSocketReceiveResult.Count)
.ToArray();

//Because we know that is a string, we convert it.
string receiveString =
//System.Text.Encoding.UTF8.GetString(payloadData, 0, payloadData.Length);
System.Text.Encoding.UTF8.GetString(payloadData, 0, payloadData.Length);

//Converts string to byte array.
var data = new
{
content = receiveString,
time = DateTime.Now.ToString()
};
SerializerHelper serializerHelper = new SerializerHelper();
var newString = serializerHelper.GetJsonString(data);
//String.Format("Hello, " + receiveString + " ! Time {0}", DateTime.Now.ToString());
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(newString);

//Sends data back.
await webSocket.SendAsync(new ArraySegment<byte>(bytes),
WebSocketMessageType.Text, true, cancellationToken);
}
}
}

public bool IsReusable
{
get
{
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="2.0.20710.0" targetFramework="net4" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net4" requireReinstallation="true" />
<package id="Microsoft.Net.Http.zh-Hans" version="2.0.20710.0" targetFramework="net4" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net4" />
<package id="Modernizr" version="2.5.3" targetFramework="net4" />
<package id="Newtonsoft.Json" version="4.5.6" targetFramework="net4" />
<package id="Owin" version="1.0" targetFramework="net45" />
<package id="WebGrease" version="1.1.0" targetFramework="net4" />
<package id="ZXing.Net" version="0.14.0.1" targetFramework="net4" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"pages":[
"pages/index/index",
"pages/logs/logs"
"pages/logs/logs",
"pages/websocket/websocket"
],
"window":{
"backgroundTextStyle":"light",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Page({
url: '../logs/logs'
})
},

bindWebsocketTap: function(){
wx.navigateTo({
url: '../websocket/websocket'
})
},

//处理wx.request请求
doRequest:function(){
var that = this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<text>{{time}}</text>
</view>


<!-- WebSocket -->
<button type="devalut" bindtap="bindWebsocketTap">
WebSocket
</button>

<!-- 进入客服 -->
<view class="btn-contact">
<contact-button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

.usermotto {
margin-top: 200px;
margin-top: 50px;
}

#clientTime{
Expand Down
Loading