话不多说,先上图
背景:
微信聊天,经常会遇见视频发不了,嗯,还有聊天不方便的问题,于是我就自己买了服务器,部署了一套可以直接在微信打开的网页进行聊天,这样只需要发送个url给朋友,就能聊天了!
由于自己无聊弄着玩的,代码比较粗糙,各位多指正!
1、首先安装SignalR,这步我就不做过多说明了
安装好以后在根目录新建一个Hubs文件夹,做用户的注册和通知
MessageHub.cs 文件
using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web; namespace SignalR.Hubs { [HubName("MessageHub")] public class MessageHub : Hub { private readonly ChatTicker ticker; public MessageHub() { ticker = ChatTicker.Instance; } public void register(string username, string group = "default") { var list = (List)HttpRuntime.Cache.Get("msg_hs"); if (list == null) { list = new List (); } if (list.Any(x => x.connectionId == Context.ConnectionId)) { Clients.Client(Context.ConnectionId).broadcastMessage("已经注册,无需再次注册"); } else if (list.Any(x => x.name == username)) { var model = list.Where(x => x.name == username && x.group == group).FirstOrDefault(); if (model != null) { //注册到全局 ticker.GlobalContext.Groups.Add(Context.ConnectionId, group); Clients.Client(model.connectionId).exit(); ticker.GlobalContext.Groups.Remove(model.connectionId, group); list.Remove(model); model.connectionId = Context.ConnectionId; list.Add(model); Clients.Group(group).removeUserList(model.connectionId); Thread.Sleep(200); var gourpList = list.Where(x => x.group == group).ToList(); Clients.Group(group).appendUserList(Context.ConnectionId, gourpList); HttpRuntime.Cache.Insert("msg_hs", list); // Clients.Client(model.connectionId).broadcastMessage("名称重复,只能注册一个"); } //Clients.Client(Context.ConnectionId).broadcastMessage("名称重复,只能注册一个"); } else { list.Add(new SiginalRModel() { name = username, group = group, connectionId = Context.ConnectionId }); //注册到全局 ticker.GlobalContext.Groups.Add(Context.ConnectionId, group); Thread.Sleep(200); var gourpList = list.Where(x => x.group == group).ToList(); Clients.Group(group).appendUserList(Context.ConnectionId, gourpList); HttpRuntime.Cache.Insert("msg_hs", list); } } public void Say(string msg) { var list = (List )HttpRuntime.Cache.Get("msg_hs"); if (list == null) { list = new List (); } var userModel = list.Where(x => x.connectionId == Context.ConnectionId).FirstOrDefault(); if (userModel != null ) { Clients.Group(userModel.group).Say(userModel.name, msg); } } public void Exit() { OnDisconnected(true); } public override Task OnDisconnected(bool s) { var list = (List )HttpRuntime.Cache.Get("msg_hs"); if (list == null) { list = new List (); } var closeModel = list.Where(x => x.connectionId == Context.ConnectionId).FirstOrDefault(); if (closeModel != null) { list.Remove(closeModel); Clients.Group(closeModel.group).removeUserList(Context.ConnectionId); } HttpRuntime.Cache.Insert("msg_hs", list); return base.OnDisconnected(s); } } public class ChatTicker { #region 实现一个单例 private static readonly ChatTicker _instance = new ChatTicker(GlobalHost.ConnectionManager.GetHubContext ()); private readonly IHubContext m_context; private ChatTicker(IHubContext context) { m_context = context; //这里不能直接调用Sender,因为Sender是一个不退出的“死循环”,否则这个构造函数将不会退出。 //其他的流程也将不会再执行下去了。所以要采用异步的方式。 //Task.Run(() => Sender()); } public IHubContext GlobalContext { get { return m_context; } } public static ChatTicker Instance { get { return _instance; } } #endregion } public class SiginalRModel { public string connectionId { get; set; } public string group { get; set; } public string name { get; set; } } }
我把类和方法都写到一块了,大家最好是分开!
接下来是控制器
HomeController.cs
using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Client; using SignalR.Hubs; using SignalR.ViewModels; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using Newtonsoft.Json; using System.Diagnostics; using System.Text.RegularExpressions; namespace SignalR.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetV(string v) { if (!string.IsNullOrEmpty(v)) { string url = RedisHelper.Get(v)?.ToString(); if (!string.IsNullOrEmpty(url)) { return Json(new { isOk = true, m = url }, JsonRequestBehavior.AllowGet); } return Json(new { isOk = false}, JsonRequestBehavior.AllowGet); } return Json(new { isOk = false }, JsonRequestBehavior.AllowGet); } public ActionResult getkey(string url) { if (!string.IsNullOrEmpty(url)) { var s = "v" + Util.GetRandomLetterAndNumberString(new Random(), 5).ToLower(); var dt = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 04:00:00")); int min = Convert.ToInt16((dt - DateTime.Now).TotalMinutes); RedisHelper.Set(s, url, min); return Json(new { isOk = true, m = s }, JsonRequestBehavior.AllowGet); } return Json(new { isOk = false }, JsonRequestBehavior.AllowGet); } public ActionResult upfile() { try { if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file != null) { var imgList = new List() { ".gif", ".jpg", ".bmp", ".png" }; var videoList = new List () { ".mp4" }; FileModel fmodel = new FileModel(); string name = Guid.NewGuid().ToString(); string fileExt = Path.GetExtension(file.FileName).ToLower();//上传文件扩展名 string path = Server.MapPath("~/files/") + name + fileExt; file.SaveAs(path); string extension = new FileInfo(path).Extension; if (extension == ".mp4") { fmodel.t = 2; } else if (imgList.Contains(extension)) { fmodel.t = 1; } else { fmodel.t = 0; } string url = Guid.NewGuid().ToString(); fmodel.url = "http://" + Request.Url.Host; if (Request.Url.Port != 80) { fmodel.url += ":" + Request.Url.Port; } fmodel.url += "/files/" + name + fileExt; GetImageThumb(Server.MapPath("~") + "files\\" + name + fileExt, name); return Json(new { isOk = true, m = "file:" + JsonConvert.SerializeObject(fmodel) }, JsonRequestBehavior.AllowGet); } } } catch(Exception ex) { Log.Info(ex); } return Content(""); } public string GetImageThumb(string localVideo,string name) { string path = AppDomain.CurrentDomain.BaseDirectory; string ffmpegPath = path + "/ffmpeg.exe"; string oriVideoPath = localVideo; int frameIndex = 5; int _thubWidth; int _thubHeight; GetMovWidthAndHeight(localVideo, out _thubWidth, out _thubHeight); int thubWidth = 200; int thubHeight = _thubWidth == 0 ? 200 : (thubWidth * _thubHeight / _thubWidth ); string thubImagePath = path + "files\\" + name + ".jpg"; string command = string.Format("\"{0}\" -i \"{1}\" -ss {2} -vframes 1 -r 1 -ac 1 -ab 2 -s {3}*{4} -f image2 \"{5}\"", ffmpegPath, oriVideoPath, frameIndex, thubWidth, thubHeight, thubImagePath); Cmd.RunCmd(command); return name; } /// /// 获取视频的帧宽度和帧高度 /// /// mov文件的路径 ///null表示获取宽度或高度失败 public static void GetMovWidthAndHeight(string videoFilePath, out int width, out int height) { try { //执行命令获取该文件的一些信息 string ffmpegPath = AppDomain.CurrentDomain.BaseDirectory + "/ffmpeg.exe"; string output; string error; ExecuteCommand("\"" + ffmpegPath + "\"" + " -i " + "\"" + videoFilePath + "\"", out output, out error); if (string.IsNullOrEmpty(error)) { width = 0; height = 0; } //通过正则表达式获取信息里面的宽度信息 Regex regex = new Regex("(\\d{2,4})x(\\d{2,4})", RegexOptions.Compiled); Match m = regex.Match(error); if (m.Success) { width = int.Parse(m.Groups[1].Value); height = int.Parse(m.Groups[2].Value); } else { width = 0; height = 0; } } catch (Exception) { width = 0; height = 0; } } public static void ExecuteCommand(string command, out string output, out string error) { try { //创建一个进程 Process pc = new Process(); pc.StartInfo.FileName = command; pc.StartInfo.UseShellExecute = false; pc.StartInfo.RedirectStandardOutput = true; pc.StartInfo.RedirectStandardError = true; pc.StartInfo.CreateNoWindow = true; //启动进程 pc.Start(); //准备读出输出流和错误流 string outputData = string.Empty; string errorData = string.Empty; pc.BeginOutputReadLine(); pc.BeginErrorReadLine(); pc.OutputDataReceived += (ss, ee) => { outputData += ee.Data; }; pc.ErrorDataReceived += (ss, ee) => { errorData += ee.Data; }; //等待退出 pc.WaitForExit(); //关闭进程 pc.Close(); //返回流结果 output = outputData; error = errorData; } catch (Exception) { output = null; error = null; } } } public class Util { public static string GetRandomLetterAndNumberString(Random random, int length) { if (length < 0) { throw new ArgumentOutOfRangeException("length"); } char[] pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; string result = ""; int n = pattern.Length; for (int i = 0; i < length; i++) { int rnd = random.Next(0, n); result += pattern[rnd]; } return result; } } class Cmd { private static string CmdPath = @"C:\Windows\System32\cmd.exe"; ////// 执行cmd命令 返回cmd窗口显示的信息 /// 多命令请使用批处理命令连接符: /// /// /// 执行的命令 public static string RunCmd(string cmd) { cmd = cmd.Trim().TrimEnd('&') + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态 using (Process p = new Process()) { p.StartInfo.FileName = CmdPath; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true; //重定向标准错误输出 p.StartInfo.CreateNoWindow = true; //不显示程序窗口 p.Start();//启动程序 //向cmd窗口写入命令 p.StandardInput.WriteLine(cmd); p.StandardInput.AutoFlush = true; //获取cmd窗口的输出信息 string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();//等待程序执行完退出进程 p.Close(); return output; } } } }
我还是都写到一块了,大家记得分开!
SController.cs 这个是针对手机端单独拎出来的,里面不需要什么内容
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace SignalR.Controllers { public class SController : Controller { // GET: S public ActionResult Index() { return View(); } } }
根目录新建一个ViewModels文件夹,里面新建FileModel.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SignalR.ViewModels { public class FileModel { ////// 1 : 图片 2:视频 /// public int t { get; set; } public string url { get; set; } } }
RedisHelper.cs
using Microsoft.AspNet.SignalR.Messaging; using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Formatters.Binary; using System.Threading.Tasks; using System.Web; namespace SignalR { public class RedisHelper { private static string Constr = "xxxx.cn:6379"; private static object _locker = new Object(); private static ConnectionMultiplexer _instance = null; ////// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。 /// public static ConnectionMultiplexer Instance { get { if (Constr.Length == 0) { throw new Exception("连接字符串未设置!"); } if (_instance == null) { lock (_locker) { if (_instance == null || !_instance.IsConnected) { _instance = ConnectionMultiplexer.Connect(Constr); } } } //注册如下事件 _instance.ConnectionFailed += MuxerConnectionFailed; _instance.ConnectionRestored += MuxerConnectionRestored; _instance.ErrorMessage += MuxerErrorMessage; _instance.ConfigurationChanged += MuxerConfigurationChanged; _instance.HashSlotMoved += MuxerHashSlotMoved; _instance.InternalError += MuxerInternalError; return _instance; } } static RedisHelper() { } ////// /// ///public static IDatabase GetDatabase() { return Instance.GetDatabase(); } /// /// 这里的 MergeKey 用来拼接 Key 的前缀,具体不同的业务模块使用不同的前缀。 /// /// ///private static string MergeKey(string key) { return "SignalR:"+ key; //return BaseSystemInfo.SystemCode + key; } /// /// 根据key获取缓存对象 /// ////// /// public static T Get (string key) { key = MergeKey(key); return Deserialize (GetDatabase().StringGet(key)); } /// /// 根据key获取缓存对象 /// /// ///public static object Get(string key) { key = MergeKey(key); return Deserialize
总体项目结构是这样的
下期我将把前端代码列出来,这个我只是为了实现功能,大神勿喷