基本信息
源码名称:SignalR服务端与客户端的消息传递
源码大小:0.80M
文件格式:.zip
开发语言:C#
更新时间:2015-08-25
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
web服务端代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SignalRServer.Index" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Signalr Chat Messenger</title> <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script> <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script></head> </head> <body> <form id="form1" runat="server"> <script type="text/javascript"> $(function () { var IWannaChat = $.connection.myChatHub; IWannaChat.client.addMessage = function (message) { $('#listMessages').append('<li>' message '</li>'); }; $("#SendMessage").click(function () { IWannaChat.server.send($('#txtMessage').val()); }); $.connection.hub.start(); }); </script> <div> <input type="text" id="txtMessage" /> <input type="button" id="SendMessage" value="广播" /> <ul id="listMessages"> </ul> </div> </form> </body> </html>
winform客户端代码如下:
public partial class FormMain : Form { HubConnection hubConnection; IHubProxy hubProxy; private delegate void AddTxt(string msg); public FormMain() { InitializeComponent(); hubConnection = new HubConnection("http://localhost:2154/signalr/hubs"); hubProxy = hubConnection.CreateHubProxy("myChatHub"); hubProxy.On<string>("addMessage", (message) => this.Invoke(new AddTxt(Show), message)); hubConnection.Start().Wait(); } private void Show(string msg) { this.txtMsg.Text = msg "\r\n"; } private void btnSend_Click(object sender, EventArgs e) { hubProxy.Invoke("send", this.txtSend.Text).Wait(); } }