做个小工具-WebSocket客户端

本文介绍了一个基于WatsonWebsocket组件开发的WebSocket客户端应用。该客户端具备连接管理、数据发送接收及编码格式选择等功能。

由于工作的原因经常需要用到一些socket,串口等调试工具,但是好多工具要么只有其中几个或者各种收费,不断提醒捐助等。所以还是自己做一个吧。毕竟也不复杂。今天先做个WebSocket客户端。WebSocket使用了开源组件WatsonWebsocket。

  1. 先上图
    在这里插入图片描述
  2. View的代码
<local:PageWithId x:Class="ToolsAssistant.Views.WebSocketClientView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ToolsAssistant.Views" xmlns:viewmodels="clr-namespace:ToolsAssistant.ViewModels" d:DataContext="{d:DesignInstance Type=viewmodels:WebSocketClientViewModel}"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800" Unloaded="Page_Unloaded" Loaded="Page_Loaded"
      Title="WebServerClientView" Background="White">
    
    <Grid>
        <Grid.Resources>
            <Style x:Key="btn_style" TargetType="Button">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Content, ElementName=btn_connect}" Value="连接">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Content, ElementName=btn_connect}" Value="断开">
                        <Setter Property="IsEnabled" Value="true"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="radio_style" TargetType="RadioButton">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Content, ElementName=btn_connect}" Value="连接">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Content, ElementName=btn_connect}" Value="断开">
                        <Setter Property="IsEnabled" Value="true"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border BorderThickness="1" BorderBrush="Gray" Margin="3" CornerRadius="5" Grid.Row="0">
            <StackPanel  Orientation="Horizontal">
                <Label Content="ws://" Margin="3" VerticalContentAlignment="Center"></Label>
                <TextBox Text="{Binding Url}" Margin="3" Width="200" VerticalContentAlignment="Center"/>
                <Button x:Name="btn_connect" Margin="3" Content="{Binding ConnectString}" Width="60" Command="{Binding ConnectCommand}">
                </Button>
            </StackPanel>
        </Border>
        <Border BorderThickness="1" BorderBrush="Gray" Margin="3" CornerRadius="5" Grid.Row="1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <Button Content="发送" Command="{Binding SendCommand}" Width="60" Margin="3" Style="{StaticResource btn_style}">
                    </Button>
                    <Button Content="清空" Command=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pluto li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值