zoukankan      html  css  js  c++  java
  • C#+GoEasy实现Web实时消息后台服务器推送

    第一步:appsettings.json配置GoEasy所需参数

    "GoEasy": {
        "URL": "https://rest-hangzhou.goeasy.io/publish",
        "Appkey": "BC-**************************"
      }

    第二步:添加GoEasy发送消息公共方法

    using Dw.Util.Helper;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace Dw.BLL.Other
    {
        /// <summary>
        /// GoEasy相关方法
        /// </summary>
        public class Other_GoEasyBLL
        {
            string message = "";
    
            #region 发送消息
            /// <summary>
            /// 发送消息
            /// </summary>
            /// <param name="channel">channel</param>
            /// <param name="content">您要发送的消息内容</param>
            /// <returns></returns>
            public string SendMessage(string channel,string content)
            {
                try
                {
                    string postDataStr = "appkey="+ ConfigHelper.GetValue("GoEasy:Appkey") + "&channel=" + channel + "&content=" + content;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ConfigHelper.GetValue("GoEasy:URL"));
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                    request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
                    Stream myRequestStream = request.GetRequestStream();
                    byte[] data = Encoding.UTF8.GetBytes(postDataStr);
                    myRequestStream.Write(data, 0, data.Length);
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                    string retString = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                    myResponseStream.Close();
                    message = "success";
                }
                catch (Exception ex)
                {
                    message = ex.Message.ToString();
                    throw;
                }
                return message;
            }
            #endregion
        }
    }

    第三步:接口测试

      #region GoEasy实现聊天
            /// <summary>
            /// GoEasy实现聊天
            /// </summary>
            /// <returns></returns>
            [Route("GoEasySendMessage")]
            [HttpGet]
            public ActionResult GoEasySendMessage(string content)
            {
                string msg = other_GoEasyBLL.SendMessage("my_channel",content);
                if(msg== "success")
                {
                    return Ok(new { code = "200", res = new { msg = "发送成功!" } });
                }
                return Ok(new { code = "400", res = new { msg = msg} });
            }
            #endregion
  • 相关阅读:
    idea 编程字体推荐
    推荐!国外程序员整理的系统管理员资源大全
    jquery阻止事件冒泡的3种方式
    web前端打印总结
    前端打印插件
    object实现小老鼠交互
    前端性能优化(DOM篇)
    输入框获得焦点时外边框颜色改变
    webstorm配置scss自动编译路径
    微信开发测试号配置
  • 原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/13091551.html
Copyright © 2011-2022 走看看