日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

应用SilverLight 2.0 BETA 2的 支持回调的在线聊天室(二)

發布時間:2025/6/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 应用SilverLight 2.0 BETA 2的 支持回调的在线聊天室(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面我們來接著做客戶端:
1.在Solution中添加SiliverLight Project,命名為ChatClient
這里,我將對應的網站 放在WCF Service中

2.先在ChatClient工程中 添加DLL引用:
System.Runtime.Serialization.dll
System.ServiceModel.dll
System.ServiceModel.PollingDuplex.dll

3.先繪制一個簡單界面,修改Page.xaml 如下:
<UserControl?x:Class="ChatClient.Page"
????xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
????xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"?
????Width
="655"?Height="420">
????
<Grid?x:Name="LayoutRoot"
??????????Background
="White"
??????????ShowGridLines
="True">
????????
<Grid.RowDefinitions>
????????????
<RowDefinition?/>
????????????
<RowDefinition?Height="40"?/>
????????
</Grid.RowDefinitions>

????????
<Grid?ShowGridLines="True"
??????????????Grid.Row
="0">
????????????
<Grid.ColumnDefinitions>
????????????????
<ColumnDefinition?/>
????????????????
<ColumnDefinition?Width="150"?/>
????????????
</Grid.ColumnDefinitions>

????????????
<!--聊天內容-->
????????????
<!--TODO:加入滾動條-->
????????????
<TextBlock?x:Name="txt_CharText"
???????????????????????Grid.Column
="0"
???????????????????????Grid.Row
="0"
???????????????????????VerticalAlignment
="Center"
???????????????????????HorizontalAlignment
="Center"?/>
????????????
????????????
<!--用戶列表-->
????????????
<!--TODO:加入滾動條?ListView-->
????????????
<TextBlock?x:Name="txt_lstChatters"
???????????????????????Grid.Column
="1"
???????????????????????VerticalAlignment
="Top"
???????????????????????HorizontalAlignment
="Center"?/>
????????
</Grid>

????????
<Grid?Grid.Row="1"
??????????????ShowGridLines
="True">
????????????
<Grid.ColumnDefinitions>
????????????????
<ColumnDefinition?/>
????????????????
<ColumnDefinition?Width="75"?/>
????????????????
<ColumnDefinition?Width="150"?/>
????????????????
<ColumnDefinition?Width="75"?/>
????????????
</Grid.ColumnDefinitions>

????????????
<!--輸入對話框-->
????????????
<TextBox?x:Name="tb_Message"
?????????????????????Grid.Column
="0"
?????????????????????Margin
="5,5,5,5"
?????????????????????KeyDown
="tb_Message_KeyDown"/>

????????????
<!--發送按鈕-->
????????????
<Button?x:Name="btn_Say"
????????????????????Grid.Column
="1"
????????????????????Width
="70"
????????????????????Height
="30"
????????????????????HorizontalAlignment
="Center"
????????????????????VerticalAlignment
="Center"
????????????????????Click
="btn_Say_Click">
????????????????
<TextBlock?HorizontalAlignment="Center"
???????????????????????????VerticalAlignment
="Center">Send</TextBlock></Button>
????????????
????????????
<!--名字輸入對話框-->
????????????
<TextBox?x:Name="tb_Name"
?????????????????????Grid.Column
="2"
?????????????????????Margin
="5,5,5,5"
?????????????????????Text
="Guest"
?????????????????????KeyDown
="tb_Name_KeyDown"/>
????????????

????????????
<!--Connect按鈕-->
????????????
<Button?x:Name="btn_Connect"
????????????????????Grid.Column
="3"
????????????????????Width
="70"
????????????????????Height
="30"
????????????????????HorizontalAlignment
="Center"
????????????????????VerticalAlignment
="Center"
????????????????????Click
="btn_Connect_Click">
????????????????
<TextBlock?HorizontalAlignment="Center"
???????????????????????????VerticalAlignment
="Center">Connect</TextBlock>
????????????
</Button>

????????
</Grid>
????
</Grid>
</UserControl>
為了方便用戶輸入,在兩個TextBox中 加入了KeyDown事件 用來截獲"回車"符

4.在相應的 Page.xaml.cs中 添加如下引用:
using?System.ServiceModel;
using?System.ServiceModel.Channels;
using?System.Threading;
5.添加如下代碼,創建Channel
????????SynchronizationContext?uiThread;
????????IDuplexSessionChannel?m_Channel;

????????
public?void?InitChannel()
????????
{
????????????
//?Grab?a?reference?to?the?UI?thread.
????????????uiThread?=?SynchronizationContext.Current;

????????????uiThread.Post(WriteText,?
"Connecting"?+?Environment.NewLine);

????????????
//?Instantiate?the?binding?and?set?the?time-outs.
????????????PollingDuplexHttpBinding?binding?=?new?PollingDuplexHttpBinding()
????????????
{
????????????????PollTimeout?
=?TimeSpan.FromSeconds(10),
????????????????InactivityTimeout?
=?TimeSpan.FromMinutes(1)
????????????}
;

????????????
//?Instantiate?and?open?channel?factory?from?binding.
????????????IChannelFactory<IDuplexSessionChannel>?factory?=
????????????????binding.BuildChannelFactory
<IDuplexSessionChannel>(new?BindingParameterCollection());

????????????IAsyncResult?factoryOpenResult?
=
????????????????factory.BeginOpen(
new?AsyncCallback(OnOpenCompleteFactory),?factory);
????????????
if?(factoryOpenResult.CompletedSynchronously)
????????????
{
????????????????CompleteOpenFactory(factoryOpenResult);
????????????}

????????}
這里利用
SynchronizationContext.Current.Post() 異步向頁面輸送信息

6.加如相應的完成事件
void?OnOpenCompleteFactory(IAsyncResult?result)
????????
{
????????????
if?(result.CompletedSynchronously)
????????????????
return;
????????????
else
????????????????CompleteOpenFactory(result);

????????}


????????
void?CompleteOpenFactory(IAsyncResult?result)
????????
{
????????????IChannelFactory
<IDuplexSessionChannel>?factory?=
????????????????(IChannelFactory
<IDuplexSessionChannel>)result.AsyncState;

????????????factory.EndOpen(result);

????????????
//?The?factory?is?now?open.?Create?and?open?a?channel?from?the?channel?factory.
????????????IDuplexSessionChannel?channel?=
????????????????factory.CreateChannel(
new?EndpointAddress("http://192.168.0.13:18600/ChatService.svc"));

????????????IAsyncResult?channelOpenResult?
=
????????????????channel.BeginOpen(
new?AsyncCallback(OnOpenCompleteChannel),?channel);
????????????
if?(channelOpenResult.CompletedSynchronously)
????????????
{
????????????????CompleteOpenChannel(channelOpenResult);
????????????}

????????}


????????
void?OnOpenCompleteChannel(IAsyncResult?result)
????????
{
????????????
if?(result.CompletedSynchronously)
????????????????
return;
????????????
else
????????????????CompleteOpenChannel(result);
????????}


????????
void?CompleteOpenChannel(IAsyncResult?result)
????????
{
????????????IDuplexSessionChannel?channel?
=?(IDuplexSessionChannel)result.AsyncState;

????????????channel.EndOpen(result);

????????????
this.m_Channel?=?channel;

????????????
//?The?channel?is?now?open.?Send?a?message.
????????????Message?message?=?Message.CreateMessage(channel.GetProperty<MessageVersion>(),?"Silverlight/IChat/JoinChat",?this.tb_Name.Text.ToString());
????????????IAsyncResult?resultChannel?
=?channel.BeginSend(message,?new?AsyncCallback(OnSend),?channel);
????????????
if?(resultChannel.CompletedSynchronously)
????????????
{
????????????????CompleteOnSend(resultChannel);
????????????}


????????????
//?Also?start?the?receive?loop?to?listen?for?callbacks?from?the?service.
????????????ReceiveLoop(channel);
????????}


注意:
(1)這里的
IDuplexSessionChannel?channel?=
????????????????factory.CreateChannel(
new?EndpointAddress("http://192.168.0.13:18600/ChatService.svc")); 這個地址應該添入,WCF Service的地址,不要忘記端口號

(2)利用
IDuplexSessionChannel.BeginSend()方法 與服務器通信

7.完成與服務器通信的相應調用
?void?OnSend(IAsyncResult?result)
????????
{
????????????
if?(result.CompletedSynchronously)
????????????????
return;
????????????
else
????????????????CompleteOnSend(result);
????????}



????????
void?CompleteOnSend(IAsyncResult?result)
????????
{
????????????IDuplexSessionChannel?channel?
=?(IDuplexSessionChannel)result.AsyncState;

????????????channel.EndSend(result);?
????????}


????????
void?ReceiveLoop(IDuplexSessionChannel?channel)
????????
{
????????????
//?Start?listening?for?callbacks.
????????????IAsyncResult?result?=?channel.BeginReceive(new?AsyncCallback(OnReceiveComplete),?channel);
????????????
if?(result.CompletedSynchronously)
????????????????CompleteReceive(result);
????????}


????????
void?OnReceiveComplete(IAsyncResult?result)
????????
{
????????????
if?(result.CompletedSynchronously)
????????????????
return;
????????????
else
????????????????CompleteReceive(result);
????????}


????????
void?CompleteReceive(IAsyncResult?result)
????????
{
????????????
//?A?callback?was?received.
????????????IDuplexSessionChannel?channel?=?(IDuplexSessionChannel)result.AsyncState;

????????????
try
????????????
{
????????????????Message?receivedMessage?
=?channel.EndReceive(result);

????????????????
if?(receivedMessage?==?null)
????????????????
{
????????????????????
//?Server?closed?its?output?session,?can?close?client?channel,?
????????????????????
//?or?continue?sending?messages?to?start?a?new?session.
????????????????}

????????????????
else
????????????????
{
????????????????????
//?Show?the?service?response?in?the?UI.
????????????????????string?text?=?receivedMessage.GetBody<string>();
????????????????????uiThread.Post(WriteText,?text?
+?Environment.NewLine);
????????????????????ReceiveLoop(channel);
????????????????}

????????????}

????????????
catch?(CommunicationObjectFaultedException)
????????????
{
????????????????
//?The?channel?inactivity?time-out?was?reached.
????????????}


????????}


????????
private?void?SendMessage(string?text)
????????
{
????????????
//?The?channel?is?now?open.?Send?a?message
????????????Message?message?=?Message.CreateMessage(this.m_Channel.GetProperty<MessageVersion>(),?"Silverlight/IChat/SayChat",?text);
????????????IAsyncResult?resultChannel?
=?this.m_Channel.BeginSend(message,?new?AsyncCallback(OnSend),?this.m_Channel);
????????????
if?(resultChannel.CompletedSynchronously)
????????????
{
????????????????CompleteOnSend(resultChannel);
????????????}


????????????
//?Also?start?the?receive?loop?to?listen?for?callbacks?from?the?service.
????????????ReceiveLoop(this.m_Channel);
????????}

8.加入前臺邏輯處理
????????private?void?btn_Say_Click(object?sender,?RoutedEventArgs?e)
????????
{
????????????SendMessage(
this.tb_Message.Text.ToString());
????????????
this.tb_Message.Text?=?"";
????????}


????????
private?void?btn_Connect_Click(object?sender,?RoutedEventArgs?e)
????????
{
????????????InitChannel();
????????}


????????
private?void?tb_Message_KeyDown(object?sender,?KeyEventArgs?e)
????????
{
????????????
if?(e.Key?==?Key.Enter)
????????????
{
????????????????SendMessage(
this.tb_Message.Text.ToString());
????????????????
this.tb_Message.Text?=?"";
????????????}

????????}


????????
private?void?tb_Name_KeyDown(object?sender,?KeyEventArgs?e)
????????
{
????????????
if?(e.Key?==?Key.Enter)
????????????
{
????????????????InitChannel();
????????????}

????????}


????????
void?WriteText(object?text)
????????
{
????????????
this.txt_CharText.Text?+=?(string)text;
????????}

***********************************華麗的分割線**************************************

至此 一個支持回調的SiliverLight 2.0 聊天室 就完成了,從中間可以看出,SiliverLight對WCF的支持還是不夠好,
它并不能連接TCP的WCF Service Host 這樣,它所支持的所謂CallBack實際上 是SiliverLight內部自己的輪詢,
在本質上,似乎還是沒有什么重大的突破,只是做的更有效率一點而已,但是SiliverLight所支持的多線程操作,
使其有更好的可發展空間,這一點,也是讓大家十分高興的~畢竟不會象原來那樣,因為種種原因,整個瀏覽器崩掉~

*****************************

應要求 現在補上我的Sulotion? :?? Download

轉載于:https://www.cnblogs.com/monkeyboyabc/archive/2008/07/14/1242520.html

總結

以上是生活随笔為你收集整理的应用SilverLight 2.0 BETA 2的 支持回调的在线聊天室(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。