C# 将实时推文流到my.net网站
我正在开发一个基于web的应用程序,其中我希望在特定查询上显示twitter流。用户确实需要刷新网页视图,它会自动加载推文 到目前为止,我已经使用tweetinvi创建了一个简单的控制台应用程序,它读取tweets并在tweets上执行所有自定义逻辑C# 将实时推文流到my.net网站,c#,asp.net-web-api,stream,tweetinvi,C#,Asp.net Web Api,Stream,Tweetinvi,我正在开发一个基于web的应用程序,其中我希望在特定查询上显示twitter流。用户确实需要刷新网页视图,它会自动加载推文 到目前为止,我已经使用tweetinvi创建了一个简单的控制台应用程序,它读取tweets并在tweets上执行所有自定义逻辑 接下来,我需要知道如何创建项目布局/基础设施,使我的web应用程序在没有客户端交互的情况下获得持续的提要 您需要将实时数据推送到浏览器。使用 SignalR是ASP.NET的优秀库,允许您编写实时web应用程序。它使用的是隐藏的,但对于较旧的浏览器
接下来,我需要知道如何创建项目布局/基础设施,使我的web应用程序在没有客户端交互的情况下获得持续的提要 您需要将实时数据推送到浏览器。使用
SignalR是ASP.NET的优秀库,允许您编写实时web应用程序。它使用的是隐藏的,但对于较旧的浏览器有许多备用位置 您需要将实时数据推送到浏览器。使用
SignalR是ASP.NET的优秀库,允许您编写实时web应用程序。它使用的是隐藏的,但对于较旧的浏览器有许多备用位置 正如内森·库珀所指出的,信号机是实现这一目标的最佳方式。由于我刚刚构建了您所描述的内容,我将向您详细介绍您需要做的事情 创建一个新的ASP.NET MVC项目,并使用NuGet和Tweetinvi安装ASP.NET Signaler 右键单击App_Start文件夹并添加一个新的OWIN启动类。如果您使用NuGet安装了SignalR,则应在上下文菜单中列出该类 您的OWIN启动类应如下所示:
[assembly: OwinStartup(typeof(TwitterClient.Web.App_Start.Startup))]
namespace TwitterClient.Web.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
public class TwitterConnection {
private string _consumerKey = ConfigurationManager.AppSettings.Get("consumerKey");
private string _consumerSecret = ConfigurationManager.AppSettings.Get("consumerSecret");
private string _accessKey = ConfigurationManager.AppSettings.Get("accessToken");
private string _accessToken = ConfigurationManager.AppSettings.Get("accessTokenSecret");
private IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<TwitterHub>();
public TwitterConnection()
{
// Access the filtered stream
var filteredStream = Stream.CreateFilteredStream();
filteredStream.MatchingTweetReceived += (sender, args) =>
{
_context.Clients.All.broadcast(args.Tweet.Text);
};
filteredStream.StartStreamMatchingAllConditions();
}
}
// document ready shorthand
$(function () {
// obtain reference to the hub proxy and hub itself
var theHub = $.connection.twitterHub;
// this is the function that the server will call to broadcast new tweets
theHub.client.broadcast = function (tweet) {
var item = '<li>' + tweet.text + '</li>';
$('ul.tweets').prepend(item);
};
// this is a function that indicates that connection to the hub has been successful
$.connection.hub.start().done(function () {
console.log("connected");
});
});
将名为Hubs的新文件夹添加到项目中,并添加一个新的signarhub类。该类还应作为模板在visualstudio的“新建文件”对话框中提供
创建一个名为TwitterConnection的新类,无论您在项目中的什么位置。在这个类的构造函数中,执行您在控制台应用程序中所做的所有操作,以使用Tweetinvi连接到twitterapi。通常,当您在SignalR中从服务器向客户机广播数据时,您可以在Hub类内进行广播,但您可以使用GlobalHost.ConnectionManager.GetHubContext在Hub类之外获得对SignalR Hub的引用;其中HUBNAME是中心的名称。因此,您的TwitterConnection类应该如下所示:
[assembly: OwinStartup(typeof(TwitterClient.Web.App_Start.Startup))]
namespace TwitterClient.Web.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
public class TwitterConnection {
private string _consumerKey = ConfigurationManager.AppSettings.Get("consumerKey");
private string _consumerSecret = ConfigurationManager.AppSettings.Get("consumerSecret");
private string _accessKey = ConfigurationManager.AppSettings.Get("accessToken");
private string _accessToken = ConfigurationManager.AppSettings.Get("accessTokenSecret");
private IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<TwitterHub>();
public TwitterConnection()
{
// Access the filtered stream
var filteredStream = Stream.CreateFilteredStream();
filteredStream.MatchingTweetReceived += (sender, args) =>
{
_context.Clients.All.broadcast(args.Tweet.Text);
};
filteredStream.StartStreamMatchingAllConditions();
}
}
// document ready shorthand
$(function () {
// obtain reference to the hub proxy and hub itself
var theHub = $.connection.twitterHub;
// this is the function that the server will call to broadcast new tweets
theHub.client.broadcast = function (tweet) {
var item = '<li>' + tweet.text + '</li>';
$('ul.tweets').prepend(item);
};
// this is a function that indicates that connection to the hub has been successful
$.connection.hub.start().done(function () {
console.log("connected");
});
});
最后,您需要连接signar的客户端元素。在MVC布局视图(即Views/Shared/_Layout.cshtml)中,在HTML标记的底部添加对SignalR JavaScript库、生成的集线器代理和外部JavaScript文件的引用,您的样板文件SignalR JavaScript将放在其中:
<!--Reference the SignalR library. -->
<script src="../../Scripts/jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<script src="../../Scripts/application.js"></script>
您的Index.cshtml文件中只有一个空文件,当收到新推文时,新推文将被预先添加到该文件中:
@{
ViewBag.Title = "Home Page";
}
<div class="row">
<div class="col-md-12">
<ul class="tweets"></ul>
</div>
</div>
正如内森·库珀所指出的,信号机是实现这一目标的最佳方式。由于我刚刚构建了您所描述的内容,我将向您详细介绍您需要做的事情 创建一个新的ASP.NET MVC项目,并使用NuGet和Tweetinvi安装ASP.NET Signaler 右键单击App_Start文件夹并添加一个新的OWIN启动类。如果您使用NuGet安装了SignalR,则应在上下文菜单中列出该类 您的OWIN启动类应如下所示:
[assembly: OwinStartup(typeof(TwitterClient.Web.App_Start.Startup))]
namespace TwitterClient.Web.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
public class TwitterConnection {
private string _consumerKey = ConfigurationManager.AppSettings.Get("consumerKey");
private string _consumerSecret = ConfigurationManager.AppSettings.Get("consumerSecret");
private string _accessKey = ConfigurationManager.AppSettings.Get("accessToken");
private string _accessToken = ConfigurationManager.AppSettings.Get("accessTokenSecret");
private IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<TwitterHub>();
public TwitterConnection()
{
// Access the filtered stream
var filteredStream = Stream.CreateFilteredStream();
filteredStream.MatchingTweetReceived += (sender, args) =>
{
_context.Clients.All.broadcast(args.Tweet.Text);
};
filteredStream.StartStreamMatchingAllConditions();
}
}
// document ready shorthand
$(function () {
// obtain reference to the hub proxy and hub itself
var theHub = $.connection.twitterHub;
// this is the function that the server will call to broadcast new tweets
theHub.client.broadcast = function (tweet) {
var item = '<li>' + tweet.text + '</li>';
$('ul.tweets').prepend(item);
};
// this is a function that indicates that connection to the hub has been successful
$.connection.hub.start().done(function () {
console.log("connected");
});
});
将名为Hubs的新文件夹添加到项目中,并添加一个新的signarhub类。该类还应作为模板在visualstudio的“新建文件”对话框中提供
创建一个名为TwitterConnection的新类,无论您在项目中的什么位置。在这个类的构造函数中,执行您在控制台应用程序中所做的所有操作,以使用Tweetinvi连接到twitterapi。通常,当您在SignalR中从服务器向客户机广播数据时,您可以在Hub类内进行广播,但您可以使用GlobalHost.ConnectionManager.GetHubContext在Hub类之外获得对SignalR Hub的引用;其中HUBNAME是中心的名称。因此,您的TwitterConnection类应该如下所示:
[assembly: OwinStartup(typeof(TwitterClient.Web.App_Start.Startup))]
namespace TwitterClient.Web.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
public class TwitterConnection {
private string _consumerKey = ConfigurationManager.AppSettings.Get("consumerKey");
private string _consumerSecret = ConfigurationManager.AppSettings.Get("consumerSecret");
private string _accessKey = ConfigurationManager.AppSettings.Get("accessToken");
private string _accessToken = ConfigurationManager.AppSettings.Get("accessTokenSecret");
private IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<TwitterHub>();
public TwitterConnection()
{
// Access the filtered stream
var filteredStream = Stream.CreateFilteredStream();
filteredStream.MatchingTweetReceived += (sender, args) =>
{
_context.Clients.All.broadcast(args.Tweet.Text);
};
filteredStream.StartStreamMatchingAllConditions();
}
}
// document ready shorthand
$(function () {
// obtain reference to the hub proxy and hub itself
var theHub = $.connection.twitterHub;
// this is the function that the server will call to broadcast new tweets
theHub.client.broadcast = function (tweet) {
var item = '<li>' + tweet.text + '</li>';
$('ul.tweets').prepend(item);
};
// this is a function that indicates that connection to the hub has been successful
$.connection.hub.start().done(function () {
console.log("connected");
});
});
最后,您需要连接signar的客户端元素。在MVC布局视图(即Views/Shared/_Layout.cshtml)中,在HTML标记的底部添加对SignalR JavaScript库、生成的集线器代理和外部JavaScript文件的引用,您的样板文件SignalR JavaScript将放在其中:
<!--Reference the SignalR library. -->
<script src="../../Scripts/jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<script src="../../Scripts/application.js"></script>
您的Index.cshtml文件中只有一个空文件,当收到新推文时,新推文将被预先添加到该文件中:
@{
ViewBag.Title = "Home Page";
}
<div class="row">
<div class="col-md-12">
<ul class="tweets"></ul>
</div>
</div>