Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

how can i make a script vb.net websocket client

How Can i create a program in vb.net to connect to my websocket server on my raspberry pi. this is so i can send commands pre-coded in the python websocket server if some one can send me code that will send "toggle" to a websocket server on 192.168.0.9:8283 then that would be all i need i have tried using some code coped from google

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:UsersAndrewDownloadsAlarm.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:UsersAndrewDownloadsAlarm.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

im wanting to send the command toggle for each timer 3 tick

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know the raspberry pi side at all, but this should work for sending the message from 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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...