一个SDK里做聊天室的例子(1)

聊天室

Option Explicit On
Option Strict On

Imports System
Imports System.IO
Imports System.Text
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

Class App
    
    'Entry point which delegates to C-style main Private Function
    Public Overloads Shared Sub Main()
        Main(System.Environment.GetCommandLineArgs())
    End Sub
    
    ' Entry point
    Overloads Public Shared Sub Main(args() As String)
        ' If the args parse in known way then run the app
        If ParseArgs(args) Then
            ' Create a custom Talker object
            Dim talkerObj As New Talker(endPoint, client)
            ' Pass the object reference to a new form object
            Dim form As New TalkForm(talkerObj)
            ' Start the talker "talking"
            talkerObj.Start()
            
            ' Run the applications message pump
            Application.Run(form)
        End If
    End Sub 'Main
    
    ' Parsed Argument Storage
    Private Shared endPoint As IPEndPoint
    Private Shared client As Boolean
    
    
    ' Parse command line arguments
    Private Shared Function ParseArgs(args() As String) As Boolean
        Try
            If args.Length = 1 Then
                client = False
                endPoint = New IPEndPoint(IPAddress.Any, 5150)
                Return True
            End If
           
            Dim port As Integer
            Select Case Char.ToUpper(args(1).ToCharArray()(1))
                Case "L"c
                    port = 5150
                    If args.Length > 2 Then
                        port = Convert.ToInt32(args(2))
                    End If
                    endPoint = New IPEndPoint(IPAddress.Any, port)
                    client = False
                Case "C"c
                    port = 5150
                    Dim address As String = "127.0.0.1"
                    client = True
                    If args.Length > 2 Then
                        address = args(2)
                        port = Convert.ToInt32(args(3))
                    End If
                    endPoint = New IPEndPoint(Dns.Resolve(address).AddressList(0), port)
                Case Else
                    ShowUsage()
                        Return False
            End Select
        Catch
        End Try
        
        Return True
    End Function 'ParseArgs
    
    
    ' Show sample usage
    Private Shared Sub ShowUsage()
        MessageBox.Show("WinTalk [switch] [parameters...]" & ControlChars.CrLf & ControlChars.CrLf & _
            "  /L  [port]" & ControlChars.Tab & ControlChars.Tab & "-- Listens on a port.  Default:  5150" & ControlChars.CrLf & _
            "  /C  [address] [port]" & ControlChars.Tab & "-- Connects to an address and port." & ControlChars.CrLf & ControlChars.CrLf & _
            "Example Server - " & ControlChars.CrLf & _
            "Wintalk /L" & ControlChars.CrLf & ControlChars.CrLf & _
            "Example Client - " & ControlChars.CrLf & _
            "Wintalk /C ServerMachine 5150", "WinTalk Usage")
    End Sub 'ShowUsage
End Class 'App

' UI class for the sample
Class TalkForm
    Inherits Form
    
    Public Sub New(talkerObj As Talker)
        ' Associate for method with the talker object
        Me.talkerObj = talkerObj
        AddHandler talkerObj.Notifications, AddressOf HandleTalkerNotifications
        
        ' Create a UI elements
        Dim talkSplitter As New Splitter()
        Dim talkPanel As New Panel()

        receiveText = New TextBox()
        sendText = New TextBox()
        
        'we'll support up to 64k data in our text box controls
        receiveText.MaxLength = 65536
        sendText.MaxLength = 65536
        
        statusText = New Label()
        
        ' Initialize UI elements
        receiveText.Dock = DockStyle.Top
        receiveText.Multiline = True
        receiveText.ScrollBars = ScrollBars.Both
        receiveText.Size = New Size(506, 192)
        receiveText.TabIndex = 1
        receiveText.Text = ""
        receiveText.WordWrap = False
        receiveText.ReadOnly = True
        
        talkPanel.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
        talkPanel.Controls.AddRange(New Control() {sendText, talkSplitter, receiveText})
        talkPanel.Size = New Size(506, 371)
        talkPanel.TabIndex = 0
        
        talkSplitter.Dock = DockStyle.Top
        talkSplitter.Location = New Point(0, 192)
        talkSplitter.Size = New Size(506, 6)
        talkSplitter.TabIndex = 2
        talkSplitter.TabStop = False
        
        statusText.Dock = DockStyle.Bottom
        statusText.Location = New Point(0, 377)
        statusText.Size = New Size(507, 15)
        statusText.TabIndex = 1
        statusText.Text = "Status:"
        
        sendText.Dock = DockStyle.Fill
        sendText.Location = New Point(0, 198)
        sendText.Multiline = True
        sendText.ScrollBars = ScrollBars.Both
        sendText.Size = New Size(506, 173)
        sendText.TabIndex = 0
        sendText.Text = ""
        sendText.WordWrap = False
        AddHandler sendText.TextChanged, AddressOf HandleTextChange
        sendText.Enabled = False
        
        AutoScaleBaseSize = New Size(5, 13)
        ClientSize = New Size(507, 392)
        Controls.AddRange(New Control() {statusText, talkPanel})
        Me.Text = "WinTalk"

        Me.ActiveControl = sendText
    End Sub 'New
    
    
    ' When the app closes, dispose of the talker object
    Protected Overrides Sub OnClosed(e As EventArgs)
        If Not (talkerObj Is Nothing) Then
            RemoveHandler talkerObj.Notifications, AddressOf HandleTalkerNotifications
            talkerObj.Dispose()
        End If
        MyBase.OnClosed(e)
    End Sub 'OnClosed
    
    
    ' Handle notifications from the talker object
    Private Sub HandleTalkerNotifications(notify As Talker.Notification, data As Object)
        Select Case notify
            Case Talker.Notification.Initialized
                ' Respond to status changes
            Case Talker.Notification.StatusChange
                Dim statusObj As Talker.Status = CType(data, Talker.Status)
                statusText.Text = String.Format("Status: {0}", statusObj)
                If statusObj = Talker.Status.Connected Then
                    sendText.Enabled = True
                End If
                ' Respond to received text
            Case Talker.Notification.Received
                receiveText.Text = data.ToString()
                receiveText.SelectionStart = Int32.MaxValue
                receiveText.ScrollToCaret()
                ' Respond to error notifications
            Case Talker.Notification.ErrorNotify
                Close(data.ToString())
                ' Respond to end
            Case Talker.Notification.EndNotify
                MessageBox.Show(data.ToString(), "Closing WinTalk")
                Close()
            Case Else
                Close()
        End Select
    End Sub 'HandleTalkerNotifications
    
    
    ' Handle text change notifications and send talk
    Private Sub HandleTextChange(sender As Object, e As EventArgs)
        If Not (talkerObj Is Nothing) Then
            talkerObj.SendTalk(CType(sender, TextBox).Text)
        End If
    End Sub 'HandleTextChange
    
    
    ' Close with an explanation
    Private OverLoads Sub Close(message As String)
        MessageBox.Show(message, "Error!")
        Close()
    End Sub 'Close
    
    ' Private UI elements
    Private receiveText As TextBox
    Private sendText As TextBox
    Private statusText As Label
    Private talkerObj As Talker

    Private Sub TalkForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub InitializeComponent()
        '
        'TalkForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "TalkForm"

    End Sub
End Class 'TalkForm

' An encapsulation of the Sockets class used for socket chatting
Class Talker
    Implements IDisposable
    
    ' Construct a talker
    Public Sub New(endPoint As IPEndPoint, client As Boolean)
        Me.endPoint = endPoint
        Me.client = client
        
        socket = Nothing
        reader = Nothing
        writer = Nothing
        
        statusText = String.Empty
        prevSendText = String.Empty
        prevReceiveText = String.Empty
    End Sub 'New
    
    
    ' Finalize a talker
    Overrides Protected Sub Finalize()
        Dispose()
        MyBase.Finalize()
    End Sub 'Finalize
    
    
    ' Dispose of resources and surpress finalization
    Public Sub Dispose() Implements IDisposable.Dispose
        GC.SuppressFinalize(Me)
        If Not (reader Is Nothing) Then
            reader.Close()
            reader = Nothing
        End If
        If Not (writer Is Nothing) Then
            writer.Close()
            writer = Nothing
        End If
        If Not (socket Is Nothing) Then
            socket.Close()
            socket = Nothing
        End If
    End Sub 'Dispose
    
    
    ' Nested delegate class and matchine event
    Delegate Sub NotificationCallback(notify As Notification, data As Object)
    Public Event Notifications As NotificationCallback
    
    
    ' Nested enum for notifications
    Public Enum Notification
        Initialized = 1
        StatusChange
        Received
        EndNotify
        ErrorNotify
    End Enum 'Notification
 

时间: 2024-12-05 10:21:42

一个SDK里做聊天室的例子(1)的相关文章

一个SDK里做聊天室的例子(2)

聊天室             ' Nested enum for supported states    Public Enum Status        Listening        Connected    End Enum 'Status            ' Start up the talker's functionality    Public Sub Start()        ThreadPool.QueueUserWorkItem(New System.Threa

实时加载-如何做一个ASP.NET MVC 聊天室

问题描述 如何做一个ASP.NET MVC 聊天室 最近在做一个项目,做一个聊天室,却遇到了我无法解决的问题主要是不知道如何实时获取聊天数据,有大神帮下忙吗? 解决方案 用signalr,http://www.cnblogs.com/kklldog/p/3198461.html

实现一个简单的语音聊天室(源码)

语音聊天室,或多人语音聊天,是即时通信应用中常见的功能之一,比如,QQ的语音讨论组就是我们用得比较多的.        这篇文章将实现一个简单的语音聊天室,让多个人可以进入同一个房间进行语音沟通.先看运行效果截图:          从左到右的三张图分别是:登录界面.语音聊天室的主界面.标注了各个控件的主界面. (如果觉得界面太丑,没关系,后面下载源码后,你可以自己美化~~) 一. C/S结构 很明显,我这个语音聊天室采用的是C/S结构,整个项目结构相对比较简单,如下所示:        该项目

实现一个简单的视频聊天室(源码)

       在 <实现一个简单的语音聊天室>一文发布后,很多朋友建议我也实现一个视频聊天室给他们参考一下,其实,视频聊天室与语音聊天室的原理是差不多的,由于加入了摄像头.视频的处理,逻辑会繁杂一些,本文就实现一个简单的多人视频聊天系统,让多个人可以进入同一个房间进行语音视频沟通.先看看3个人进行视频聊天的运行效果截图:         上面两张截图分别是:登录界面.标注了各个控件的视频聊天室的主界面. 一. C/S结构 很明显,我这个语音聊天室采用的是C/S结构,整个项目结构相对比较简单,如

刚学习C#,求一个P2P 多人聊天室源代码学习

问题描述 本人菜鸟,刚学习C#,求一个P2P多人聊天室源代码学习,求犀利源代码学习借鉴. 解决方案 解决方案二:可以发到本人邮箱451195526@qq.com谢谢~~~各位大牛!解决方案三:去51aspx网站下载

求做聊天室程序!

问题描述 主界面已经做好,求高手做一个聊天室程序.需求:可以更换头像.只要求文字聊天,私聊.公聊.高手加我QQ86129789高手有酬劳 解决方案 解决方案二:帮顶解决方案三:...解决方案四:http://www.codepub.com/software/view-software-14676.html代码还是要自己写的,像你说的"更换头像"其实就是图片上传的问题...还是自己慢慢琢磨吧....解决方案五:我这有Java版本的QQ 楼主要不解决方案六:的马士兵的在线聊天

asp+ajax做聊天室一

先说明一下这里是一个简单的聊天室,用ajax做的,共分为两个文件,index.html与post.asp 首先我们一看看index.html文件的代码, <html><head><title></title> <style>*{font:9pt 宋体;line-height:1.7}</style> <script> var XmlDom = document.createElement("xml");

asp+ajax做聊天室二

下面的代码是post.asp文件代码.比较简单所以就不说明了. <%@Language="JavaScript" CodePage="936"%><% var Nothing; Response.addHeader("Cache-Control","no-cahce"); if(!Session.Contents("Chat_User")) {     Session.Contents(&

用三五天时间边学边用rails做了一个简单的在线聊天室。

问题描述 刚开始需要一个熟悉的过程,不过之后确实能体会到rails令人清爽的一面.地址是http://219.234.86.88:3001/源代码在googlecode.