如何制作一个脚本vb.net websocket client

时间:2015-10-28 00:59:41

标签: vb.net websocket raspberry-pi client vb.net-2010

如何在vb.net中创建程序以连接到我的raspberry pi上的websocket服务器。这是我可以发送预编码在python websocket服务器中的命令,如果有人可以发送代码,将“toggle”发送到192.168.0.9:8283上的websocket服务器那么这将是我需要的所有我尝试使用一些代码来自谷歌

Imports WMPLib
Imports WebSocket4Net


Public Class Alerter
    Dim Type As String
    Dim ran As String = "NO"
    Dim mp3player As New MediaPlayer.MediaPlayer

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        If Not mp3player.PlayState = MediaPlayer.MPPlayStateConstants.mpPlaying Then
            mp3player.Open("C:\Users\Andrew\Downloads\Alarm.mp3")
        End If

        If Me.Opacity = 1.0 Then
            Type = "Back"
            If ran = "NO" Then

            End If
            ran = "NO"
        ElseIf Me.Opacity = 0.0 Then
            Type = "Foward"
            If ran = "YES" Then

            End If
        End If

        If Type = "Back" Then
            Me.Opacity = Me.Opacity - 0.1
        Else
            Me.Opacity = Me.Opacity + 0.1
        End If
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

    End Sub




    Public Async Function ShowAlert(skypename As String) As Task

        Label1.Text = skypename
        Label1.Left = (My.Computer.Screen.Bounds.Width - Label1.Width) / 2
        Label2.Left = (My.Computer.Screen.Bounds.Width - Label2.Width) / 2
        Label3.Left = (My.Computer.Screen.Bounds.Width - Label3.Width) / 2
        Label4.Left = (My.Computer.Screen.Bounds.Width - Label4.Width) / 2


        Label2.Top = (My.Computer.Screen.Bounds.Height - Label2.Height) / 2
        Label1.Top = ((My.Computer.Screen.Bounds.Height - Label1.Height) / 2) - Label2.Height - 20
        Label3.Top = ((My.Computer.Screen.Bounds.Height - Label3.Height) / 2) + Label2.Height + 20
        Label4.Top = ((My.Computer.Screen.Bounds.Height - Label4.Height) / 2) + Label2.Height + Label3.Height + 20

        Me.Show()
        Timer1.Start()
        If (Form1.CheckBox1.Checked) Then

            Timer3.Start()
        End If

        Cheacker.Start()

        mp3player.Open("C:\Users\Andrew\Downloads\Alarm.mp3")

    End Function

    Private Sub Alerter_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.Click
        mp3player.Stop()
        Timer3.Stop()
        Me.Close()
    End Sub

    Private Sub Cheacker_Tick(sender As Object, e As EventArgs) Handles Cheacker.Tick
        Dim newValue As Integer = Label4.Text - 1
        If (newValue = 0) Then
            Label4.Text = newValue
            Cheacker.Interval = 500
            sendAFK()
        ElseIf newValue < 0 Then
            If Label4.Visible = True Then
                Label4.Visible = False
            Else
                Label4.Visible = True
            End If
        Else
            Label4.Text = newValue
        End If

    End Sub

    Private Sub sendAFK()
        Cheacker.Stop()
        Dim skypename As String = Form1.TextBox1.Text
        Form1.oSkype.SendMessage(skypename, "Hello Andrew Currently Had On on A Alert List And He Curremtly Has A Siren Running And His Screen Has Been Nulled To Red With A Message Saying You Came Online. So Why Have You Been Sent This Automated Message? The Alert Has Been Running And He Has Not Responded At All To It So Please do Not Think His Is Ignoring You!")

        Label3.Text = "AFK Message Was Sent At"
        Dim time As String = My.Computer.Clock.LocalTime.Hour.ToString + ":" + My.Computer.Clock.LocalTime.Minute.ToString + ":" + My.Computer.Clock.LocalTime.Second.ToString
        Label4.Text = time
        Label1.Text = skypename
        Label1.Left = (My.Computer.Screen.Bounds.Width - Label1.Width) / 2
        Label2.Left = (My.Computer.Screen.Bounds.Width - Label2.Width) / 2
        Label3.Left = (My.Computer.Screen.Bounds.Width - Label3.Width) / 2
        Label4.Left = (My.Computer.Screen.Bounds.Width - Label4.Width) / 2


        Label2.Top = (My.Computer.Screen.Bounds.Height - Label2.Height) / 2
        Label1.Top = ((My.Computer.Screen.Bounds.Height - Label1.Height) / 2) - Label2.Height - 20
        Label3.Top = ((My.Computer.Screen.Bounds.Height - Label3.Height) / 2) + Label2.Height + 20
        Label4.Top = ((My.Computer.Screen.Bounds.Height - Label4.Height) / 2) + Label2.Height + Label3.Height + 20
    End Sub

    Private Sub Alerter_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick

    End Sub
End Class

我想为每个计时器发送命令切换3勾选

1 个答案:

答案 0 :(得分:1)

我根本不知道树莓派的一面,但这应该适用于从VB发送消息:

Imports System.Net.Sockets

Private ClientSocket As New TcpClient
Private NetStream As NetworkStream
Private BytesToSend() As Byte = System.Text.Encoding.ASCII.GetBytes("toggle") 

'Create the socket connection - call before you start Timer3
ClientSocket.Connect("192.168.0.9", 8283)
NetStream = ClientSocket.GetStream

'This is the bit to call in Timer3_Tick
NetStream.Write(BytesToSend, 0, BytesToSend.Length)
NetStream.Flush()

'Close the connection when you're finished
NetStream.Close()
ClientSocket.Close()