您的位置:首页 > 其它

Windows 8 Metro UDP通信通过 DatagramSocket实现行情的推送

2013-08-27 15:06 525 查看
metro UDP通信通过 DatagramSocket实现行情的推送,可以参考 Windows
8 Metro App开发[21]DatagramSocket与UDP

private void Timer()
{
timerActive = new DispatcherTimer();
timerActive.Tick += new EventHandler<object>(timer_active);
timerActive.Interval = new TimeSpan(0, 0, 间隔时间);
timerActive.Start();

timerRestore = new DispatcherTimer();
timerRestore.Tick += new EventHandler<object>(timer_Restore);
timerRestore.Interval = new TimeSpan(0, 0, 间隔时间);
timerRestore.Start();
}
DatagramSocket socket = null;//局部变量
private void timer_active(object sender, object e)
{
try
{
if (socket != null)
{
socket.Dispose();
}
socket = null;//局部变量
ConnSocket(参数, 参数);
}
catch
{
ConnSocket(参数,参数);
}
}

private void timer_Restore(object sender, object e)
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
for (int i = 0; i < qis.Count; i++)
{
if (qis[i].ExTime.AddSeconds(3) < DateTime.Now)
{

ListBoxItem lbi = (ListBoxItem)lb2.ItemContainerGenerator.ContainerFromIndex(qis[i].Index);
if (lbi != null)
{
Grid g = Utility.FindVisualElement<Grid>(lbi);
Grid g2 = Utility.FindVisualElement<Grid>(g);
g2.Background = new SolidColorBrush(Colors.Transparent);
TextBlock tb = Utility.FindVisualElement<TextBlock>(g2);
SolidColorBrush scb = double.Parse(qis[i].Close) - double.Parse(qis[i].Last) > 0 ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red);
tb.Foreground = scb;
qis.Remove(qis[i]);
}
}
}
});
}

private async void ConnSocket(int max, string content)
{
try
{
socket = new DatagramSocket();
if (Utility.IsConnectedToInternet())
{
HostName host = new HostName("地址");
string port = "端口";
try
{
socket.MessageReceived += MessageReceived;
await socket.BindServiceNameAsync(port);
await socket.ConnectAsync(host, port);
//将发送内容的信息存放进Socket异步事件参数中
DataWriter udpWriter = new DataWriter(socket.OutputStream);
//把数据写入到发送流
udpWriter.WriteString(content);
//异步发送
await udpWriter.StoreAsync();
//异步刷新数据
await udpWriter.FlushAsync();
// this.isConnected = true;
}
catch (Exception exception)
{
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
socket.Dispose();
return;
}
}
}
else
{
//MessageDialog dlg = new MessageDialog("未连接到网络", "友情提示");
//await dlg.ShowAsync();
}
}
catch(Exception e)
{
string mm = e.ToString();

}
}

async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
{
IBuffer buffer;
try
{
IOutputStream outputStream = await socket.GetOutputStreamAsync(eventArguments.RemoteAddress, eventArguments.RemotePort);
buffer = eventArguments.GetDataReader().DetachBuffer();
await outputStream.WriteAsync(buffer);

byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(buffer, 0, (int)buffer.Length);
string dataFromServer = Encoding.UTF8.GetString(bytes, 0, bytes.Length);//这为UDP返回的数据

await lb2.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
ObservableCollection<Quote> quotes = (ObservableCollection<Quote>)lb2.ItemsSource;
ObservableCollection<Quote> q = (ObservableCollection<Quote>)JsonConvert.DeserializeObject("[" + dataFromServer + "]", typeof(ObservableCollection<Quote>));//以下编写需要操作的数据

});
}
catch (Exception exception)
{
SocketErrorStatus socketError = SocketError.GetStatus(exception.HResult);
if (socketError == SocketErrorStatus.ConnectionResetByPeer)
{

}
else if (socketError != SocketErrorStatus.Unknown)
{

}
else
{
}
}
finally
{
socket.Dispose();
socket = null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐