Jump to content

Help with Usernames Display (Chat)


Recommended Posts

Hello, I am making another Chat but I can't ever seem to get the username display right. right now it will display your name and everyone who logs in after you do but if someone has logged in before you then it won't show their name.

Server:

#include <Array.au3>

TCPStartup()
$Port = InputBox("Port", "What port would you like to be listening to?")


Dim $Socket[1]
$Socket[0] = 0

Dim $DelSock[1]
$DelSock[0] = 0

Dim $Names[1]
$Names[0] = 0

Dim $DelNames[1]
$DelNames[0] = 0

$Server = TCPListen(@IPAddress1, $Port)
If @error Then
    MsgBox(0,"error","Error to listening on port " & $Port & " .")
    _quit()
EndIf


While 1
    Sleep(10)

    $AcceptConn = TCPAccept($Server)

    If $AcceptConn <> -1 Then
        _ArrayAdd($Socket, $AcceptConn)
        $Socket[0] += 1
    EndIf

    For $C = 1 To $Socket[0]
        If $Socket[$C] <> "" Then
            $MsgRecv = TCPRecv($Socket[$C], 512)
            If @error Then
                TCPCloseSocket($Socket[$C])
                _ArrayAdd($DelSock, $C)
                _ArrayAdd($DelNames, $C)
                $Socket[0] -= 1
                $DelSock[0] += 1

                $Names[0] -= 1
                $DelNames[0] += 1

            EndIf

            $Recv = StringSplit($MsgRecv, "~")

            If $MsgRecv <> "" Then
                If $Recv[1] = "NewUser" Then
                    _ArrayAdd($Names, $Recv[2])
                    $Names[0] += 1
                    _UpdateUsers($Recv[2])

                    _UpdateNew()
                Else
                    _ClientSend($MsgRecv)
                EndIf
            EndIf
        EndIf
    Next

    For $D2 = 1 To $DelSock[0]
        _ArrayDelete($Socket, $DelSock[$D2])
    Next

    $DelSock[0] = 0

WEnd

Func _UpdateNew()
    For $Un = 1 To $Socket[0]
        TCPSend($Socket[0], "NewUser~" & $Names[$Un])
    Next
EndFunc

Func _UpdateUsers($Name)
    For $u = 1 To $Socket[0]
    TCPSend($Socket[$u], "NewUser~" & $Name)
    Next
EndFunc

Func _ClientSend($Send)
    For $S = 1 to $Socket[0]
        If $Socket[$S] <> "" Then
            If TCPSend($Socket[$S], "[" & @Hour & ":" & @MIN & ":" & @SEC & "] " & $Send) = 0 And @error Then
                TCPCloseSocket($SOCKET[$S])
                _ArrayAdd($DelSock, $S)
                $Socket[0] -= 1
                $DelSock[0] += 1

            EndIf
        EndIf
    Next

    For $D2 = 1 To $DelSock[0]
        _ArrayDelete($Socket, $DelSock[$D2])
    Next

    $DelSock[0] = 0

EndFunc

Func _quit()
    TCPShutdown()
    Exit
EndFunc

Client:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1
 Author:         Donald8274

 Script Function:
    Client

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <INET.au3>

TCPStartup()

$Connected = 0
$Username = ""
$Connect = ""

$GUI_Main = GUICreate("Donald's Chat Client", 448, 280)

$T_Main = GUICtrlCreateTab(0, 0, 448, 280)

$T_Chat = GUICtrlCreateTabItem("Chat")

$E_Chat = GUICtrlCreateEdit("", 10, 30, 330, 180, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$E_Users = GUICtrlCreateEdit("", 350, 30, 85, 240, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_HSCROLL, $WS_VSCROLL), $WS_EX_STATICEDGE)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$E_Write = GUICtrlCreateEdit("", 10, 220, 270, 50, BitOR($ES_AUTOVSCROLL, $WS_VSCROLL), $WS_EX_STATICEDGE)
$B_Send = GUICtrlCreateButton("Send", 290, 220, 50, 50, $BS_DEFPUSHBUTTON)

$T_Connect = GUICtrlCreateTabItem("Connect")

GUICtrlSetState(-1, $GUI_SHOW)

GUICtrlCreateLabel("Server IP:", 20, 33, 50)
$I_ServerIP = GUICtrlCreateInput("", 70, 30, 100)

GUICtrlCreateLabel("Server Port:", 10, 63, 60)
$I_Port = GUICtrlCreateInput("", 70, 60, 100)

GUICtrlCreateLabel("Username:", 15, 93, 50)
$I_Username = GUICtrlCreateInput("Guest", 70, 90, 100)

$B_Connect = GUICtrlCreateButton("Connect", 70, 120, 100)

GUICtrlCreateLabel("Status:", 30, 150)
$L_Status = GUICtrlCreateLabel("Not Connected", 70, 150)
GUICtrlSetColor(-1, 0xFF0000)

GUICtrlCreateTabItem("")

If $Connected = 0 Then
    For $D = $E_Chat to $B_Send
        GUICtrlSetState($D, $GUI_DISABLE)
    Next
EndIf

GUISetState()

While 1
    sleep(10)

    $MsgGet = GUIGetMsg()
    Switch $MsgGet
        Case $GUI_EVENT_CLOSE
            _Quit()
        Case $B_Connect
            _Connect()
        Case $B_Send
            $MsgRead = GUICtrlRead($E_Write)
            $MsgSplit = StringSplit($MsgRead, " ")

            If $MsgSplit[1] = "/name" Then
                GUICtrlSetData($E_Write, "")
                GUICtrlSetState($E_Write, $GUI_FOCUS)
                TCPSend($Connect, $Username & " has changed their name to " & $MsgSplit[2] & ".")
                $Username = $MsgSplit[2]
            ElseIf $MsgSplit[1] = "/help" Then
                GUICtrlSetData($E_Write, "")
                GUICtrlSetState($E_Write, $GUI_FOCUS)
                GUICtrlSetData($E_Chat, "Commands:" & @CRLF & "/name <new name> - Change username." & @CRLF, True)
            Else
                _MsgSend()
                GUICtrlSetState($E_Write, $GUI_FOCUS)
            EndIf
    EndSwitch

    $MsgRecv = TCPRecv($Connect, 512)
    $MsgSplit2 = StringSplit($MsgRecv, "~")

    If $MsgRecv <> "" Then
        If $MsgSplit2[1] = "NewUser" Then
            MsgBox(0, "", $MsgSplit2[2])
            _UpdateUsers($MsgSplit2[2])
        Else
        $Chat = GUICtrlRead($E_Chat)
        GUICtrlSetData($E_Chat, $MsgRecv & @CRLF, True)
    EndIf
EndIf
WEnd

Func _Connect()

    $Server = GUICtrlRead($I_ServerIP)
    $Port = GUICtrlRead($I_Port)

    Global $Connect = TCPConnect($Server, $Port)
    If @error Then
        MsgBox(16, "Error", "Can not connect to " & $Server & " on port " & $Port & ".")
        MsgBox(0, "", @error)
    Else
        $Connected = 1
        GUICtrlSetData($L_Status, "Connected")
        GUICtrlSetColor($L_Status, 0x00FF00)
        $Username = GUICtrlRead($I_Username)
        If $Connected = 1 Then
            For $D = $E_Chat to $B_Send
                GUICtrlSetState($D, $GUI_ENABLE)
            Next
        EndIf
        TCPSend($CONNECT, "NewUser~" & $username & @CRLF)
        GUICtrlSetData($E_Chat, "Notice! Everything in this chat is being logged." & @CRLF & "Type '/help' for a list of commands." & @CRLF)
        TCPSend($Connect, $Username & " has connected from " & _GetIP() & ".")
    EndIf
EndFunc

Func _UpdateUsers($Name)
    GUICtrlSetData($E_Users, $Name, True)
EndFunc

Func _MsgSend()
    $Send = GUICtrlRead($E_Write)
    TCPSend($Connect, $Username & ": " & $Send)
    GUICtrlSetData($E_Write, "")
EndFunc

Func _Quit()
    TCPSend($Connect, $Username & " has disconnected.")
    TCPCloseSocket($Connect)
    TCPShutdown()
    Exit
EndFunc

I have tried to make it get everyone's name on the new client that has logged in with this part:

Func _UpdateNew()
    For $Un = 1 To $Socket[0]
        TCPSend($Socket[0], "NewUser~" & $Names[$Un])
    Next
EndFunc
Edited by Donald8282
Link to comment
Share on other sites

hi Donald8282,

Perhaps your function should look more like:

Func _UpdateNew($iNewUserIndex)
    For $Un = 1 To $Names[0]
        TCPSend($Socket[$iNewUserIndex], "NewUser~" & $Names[$Un])
    Next
EndFunc

And its call in the While loop should be:

_UpdateNew($C)

Some quick tips:

1. Try using SQLite memory databases on both client and server side, it should make data management a snap.

2. Use as little global variables as possible, instead, call your functions with parameters, so your code becomes easier to manage.

Also, you might want to take a look at this Unofficial MSNP7 protocol guide for simple examples on how to format messages for typing notifications, status changes, file transfers, different encoding etc.

Hope this helps, :unsure:

-smartee

EDIT: Malformed URL tag, Grammar, Syntax :>

Edited by smartee
Link to comment
Share on other sites

hi Donald8282,

Perhaps your function should look more like:

Func _UpdateNew($iNewUserIndex)
    For $Un = 1 To $Socket[0]
        TCPSend($Socket[$iNewUserIndex], "NewUser~" & $Names[$Un])
    Next
EndFunc

And its call in the While loop should be:

_UpdateNew($C)

Some quick tips:

1. Try using SQLite memory databases on both client and server side, it should make data management a snap.

2. Use as little global variables as possible, instead, call your functions with parameters, so your code becomes easier to manage.

Also, you might want to take a look at this Unofficial MSNP7 protocol guide for simple examples on how to format messages for typing notifications, status changes, file transfers, different encoding etc.

Hope this helps, :unsure:

-smartee

EDIT: Malformed URL tag, Grammar

Thanks, this did help but it's doing something I don't want it to.

GuestNewUser << It's displaying the names like that and not sending the ones before you connect either still =[

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...