我是靠谱客的博主 饱满秋天,最近开发中收集的这篇文章主要介绍websocket 发送json,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Module Module1

    Sub Main()
        Try
            Dim m_IMSCOMAddress As String = "ws://" + "99.99.99.99" + ":9001"
            Dim m_ws = New RemoteCmdHelper(m_IMSCOMAddress)
            m_ws.m_message = "{""message"": ""stats""}"
            m_ws.Open()
            Threading.Thread.Sleep(500)
            m_ws.Close()

            Dim str = m_ws.m_results
            '{"message":"stats","cpu":{"global":3.008392923190502},"time":117.666,"instance_id":"5d515109",
            '"cells":{"1":{"dl_bitrate":0,"ul_bitrate":0,"dl_tx":0,"ul_tx":0,"dl_retx":0,"ul_retx":0,"dl_use_min":0,
            '"dl_use_max":0.05999999865889549,"dl_use_avg":0.0031247502849712459,"ul_use_min":0.03999999910593033,
            '"ul_use_max":0.09999999403953552,"ul_use_avg":0.04600015194588735,
            '"dl_sched_users_min":0,"dl_sched_users_max":0,"dl_sched_users_avg":0,
            '"ul_sched_users_min":0,"ul_sched_users_max":0,"ul_sched_users_avg":0,
            '"ue_count_min":0,"ue_count_max":0,"ue_count_avg":0,"erab_count_min":0,
            '"erab_count_max":0,"erab_count_avg":0,"dl_gbr_use_min":0,"dl_gbr_use_max":0,
            '"dl_gbr_use_avg":0,"ul_gbr_use_min":0,"ul_gbr_use_max":0,"ul_gbr_use_avg":0}},
            '"rf_ports":{"0":{"rxtx_delay":{"min":2.597916666666667,"max":3.8399305555555556,"avg":3.3078703627033845,
            '"sd":0.2691544629708594}}},"counters":{"messages":{},"errors":{}}}
            Return
        Catch ex As Exception
            'WriteLog("CheckeNBStatus:Failed Exception" + ex.Message)
            Return
        End Try
    End Sub

End Module
/***************************************************************************************/

Imports System
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks
Imports WebSocket4Net
Imports SuperSocket
Imports SuperSocket.ClientEngine
Imports System.IO

Public Class RemoteCmdHelper
    Implements IDisposable

    Private ReceiveChunkSize As Integer = 1024
    Private SendChunkSize As Integer = 1024
    Private webSocketClient As WebSocket
    Private uri As Uri
    Private cancellationTokenSource As CancellationTokenSource = New CancellationTokenSource()
    Private cancellationToken As CancellationToken
    Public m_message As String
    Public m_results As String = ""

    Public Sub New(ByVal remoteuri As String)
        Try
            webSocketClient = New WebSocket(remoteuri)
            'AddHandler webSocketClient.Error, Sub(s, e) socketError(s, e)
            AddHandler webSocketClient.Opened, Sub(s, e) socketOpened(s, e)
            AddHandler webSocketClient.Closed, Sub(s, e) socketClosed(s, e)
            AddHandler webSocketClient.MessageReceived, Sub(s, e) socketMessage(s, e)
            AddHandler webSocketClient.DataReceived, Sub(s, e) socketDataReceived(s, e)
            'webSocketClient.AllowUnstrustedCertificate = True
        Catch ex As Exception
            'WriteLog("Fail to initialize the connect for the cmd to " + remoteuri + " Error:" + ex.Message)
        End Try
    End Sub

    Private disposedValue As Boolean ' To detect redundant calls
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                If Not IsNothing(webSocketClient) Then
                    webSocketClient.Dispose()
                End If
            End If

            Finalize()
        End If
        disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        ' TODO: uncomment the following line if Finalize() is overridden above.
        GC.SuppressFinalize(Me)
    End Sub


    Sub socketOpened(s As Object, e As EventArgs)
        'webSocketClient.Send("{'message':'ue_get'}")
        m_results = ""
        webSocketClient.Send(m_message)
    End Sub

    Sub socketClosed(s As Object, e As EventArgs)
        'WriteLog("Closed")
    End Sub

    Sub socketError(s As Object, e As IO.ErrorEventArgs)
        'WriteLog(e.Exception.ToString)
    End Sub

    Sub socketMessage(s As Object, e As WebSocket4Net.MessageReceivedEventArgs)
        m_results = e.Message
        'If m_nBaseParam1 = 724 Then
        '    WriteLog("socketMessage:" + m_results)
        '    WriteLog(e.Message)
        'End If

    End Sub

    Sub socketDataReceived(ss As Object, e As WebSocket4Net.DataReceivedEventArgs)
        Dim s As New System.Text.StringBuilder
        s.Append(System.Text.ASCIIEncoding.ASCII.GetString(e.Data, 0, e.Data.Length()))
        Dim Str = s.ToString()
        m_results = Str
        'If m_nBaseParam1 = 724 Then
        'WriteLog("socketDataReceived:" + m_results)
        'WriteLog(Str)
        'End If

    End Sub


    Public Function Open() As Boolean
        Try
            m_results = ""
            If webSocketClient.State = WebSocketState.None Or webSocketClient.State = WebSocketState.Closed Then
                webSocketClient.Open()
            End If

            Return True
        Catch ex As Exception
            'WriteLog(ex.Message)
            Return False
        End Try
    End Function

    Public Function Close() As Boolean
        Try
            If webSocketClient.State = WebSocketState.Open Then
                webSocketClient.Close()
            End If

            Return True
        Catch ex As Exception
            'WriteLog(ex.Message)
            Return False
        End Try
    End Function

    Public Function Send(ByVal data As String) As Boolean
        Try
            webSocketClient.Send(data)
            Return True
        Catch ex As Exception
            'WriteLog(ex.Message)
            Return False
        End Try
    End Function

End Class
 

最后

以上就是饱满秋天为你收集整理的websocket 发送json的全部内容,希望文章能够帮你解决websocket 发送json所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部