Author Topic: Mini Messenger  (Read 9519 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Mini Messenger
« Reply #15 on: November 19, 2019, 06:51:39 pm »
Messenger is back down.  Someone keeps connecting and disconnecting without a name, spamming Welcome messages.  Many thanks to all the fine folks who tested the code.  It was a pleasure to chat with all of you!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Mini Messenger
« Reply #16 on: November 19, 2019, 08:32:09 pm »
If anyone wants to see this concept taken to a pretty far direction, I remind everyone of Sprezzo. This codebase puts all the TCP/IP and IRC stuff under the same roof. I don't use it because of the way I connect to the internet (mobile hotspot), so the institutional knowledge of this has basically disappeared. In its hay day though, I had people using a web page to send messages to IRC, or to other webpages, or to simultaneous instances of the same program, etc, etc, etc... I tried to think of everything. Happy reading:

http://barnes.x10host.com/sprezzo/index.html
« Last Edit: November 19, 2019, 08:36:56 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Mini Messenger
« Reply #17 on: November 19, 2019, 08:53:24 pm »
I tried a number of times to connect (not able to give a name) - but had to reboot my laptop many times - as my screen mode locked me onto screen 2 and would not let me view chat as screen 0 - also my wifi/mobile hotspot kept getting interrupted and had to set up again. (This may have NOTHING to do with your messenger demo).

Marked as best answer by SMcNeill on November 19, 2019, 05:08:36 pm

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Mini Messenger
« Reply #18 on: November 19, 2019, 10:08:27 pm »
One more upgraded version of the mini messenger which works across the internet and connects to my host.

Code: [Select]
DIM SHARED OUT$, myname$, client
DIM SHARED l AS _UNSIGNED _BYTE
DIM SHARED PingTimer AS _FLOAT, LastSeen AS _FLOAT
SCREEN _NEWIMAGE(800, 600, 32)
COLOR &HFFFFFFFF, &HFF000000

PRINT "[Steve's Mini Messenger]"
client = _OPENCLIENT("TCP/IP:7993:172.93.60.23") ' Attempt to connect to local host as a client
PRINT "[connected to " + _CONNECTIONADDRESS(client) + "]"

DO
    INPUT "Enter your name: ", myname$
    myname$ = _TRIM$(myname$)
LOOP UNTIL myname$ <> ""
OUT$ = myname$ + " connected!" + CHR$(10)
PUT #client, , OUT$
PingTimer = TIMER(0.01)
LastSeen = TIMER(0.01)
DO
    GetMessage
    IF PingTimer + 30 < TIMER(0.01) THEN
        PingTimer = TIMER(0.01)
        OUT$ = "PING" + CHR$(10)
        'uncomment the below to diagnostically print when we ping the chat server
        'VIEW PRINT 1 TO 30: LOCATE 30, 1: PRINT "PING"; TIME$
        PUT #client, , OUT$
        OUT$ = ""
    END IF

    OUT$ = ""
    GetInput mymessage$ ' display current input on screen
    VIEW PRINT
    FOR i = 32 TO 35
        LOCATE i, 1: PRINT SPACE$(_WIDTH / _FONTWIDTH); ' erase previous message displayed
    NEXT
    LOCATE 32, 1: PRINT myname$ + ": "; mymessage$;
    IF INSTR(mymessage$, CHR$(10)) THEN
        OUT$ = myname$ + ":" + mymessage$
        PUT #client, , OUT$
        VIEW PRINT 1 TO 30
        LOCATE 30, 1: PRINT LEFT$(OUT$, LEN(OUT$) - 1)
        mymessage$ = ""
    END IF
    _DISPLAY
    _LIMIT 60
    IF LastSeen + 60 < TIMER(0.01) THEN 'inactive connection for too long
        VIEW PRINT 1 TO 30
        LOCATE 30, 1: PRINT "[Session Termined.  Host Timed Out.]"
        END
    END IF
LOOP

'.................... END OF MAIN PROGRAM ................


SUB GetMessage ' get & display any new message
    DO
        GET #client, , a$
        done = -1
        IF LEN(a$) > 0 THEN
            OUT$ = LTRIM$(OUT$ + a$)
            done = 0
            l = INSTR(a$, CHR$(10))
            IF l THEN
                IF OUT$ = "PONG" + CHR$(10) THEN
                    'Uncomment the following line if you want to watch the chat server talk back automatically to PING requests.
                    'VIEW PRINT 1 TO 30: LOCATE 30, 1: PRINT LEFT$(OUT$, LEN(OUT$) - 1)
                    LastSeen = TIMER(0.01)
                    OUT$ = ""
                ELSE
                    VIEW PRINT 1 TO 30: LOCATE 30, 1: PRINT LEFT$(OUT$, LEN(OUT$) - 1)
                END IF
                done = -1
            END IF
        END IF
        _LIMIT 60
    LOOP UNTIL done
END SUB

SUB GetInput (mymessage$) ' simple input handler
    k$ = INKEY$
    IF LEN(k$) THEN
        IF k$ = CHR$(8) AND LEN(mymessage$) <> 0 THEN
            mymessage$ = LEFT$(mymessage$, LEN(mymessage$) - 1)
        ELSE
            IF LEN(k$) = 1 AND ASC(k$) >= 32 THEN mymessage$ = mymessage$ + k$
        END IF
    END IF
    IF k$ = CHR$(27) OR _EXIT THEN
        OUT$ = myname$ + ": Terminated the session." 'Be nice and let the server know when we log out.
        PUT #client, , OUT$
        SYSTEM ' [Esc] key ends program
    END IF
    IF LEN(mymessage$) >= 255 THEN
        mymessage$ = mymessage$ + CHR$(10) 'autosend after 255 characters
    ELSE
        IF k$ = CHR$(13) THEN mymessage$ = mymessage$ + CHR$(10) 'so we don't send with 2 chr$(10) characters.
    END IF
END SUB

The code here is now a lot friendly for my PC... It notifies me when someone terminates a session, and it sends PINGs and PONGs every so often to make certain that both sides are still active and that the connection still exists.

I'm going to leave the chat server up and running tonight, feel free to pop in at anytime and chat with whoever might be hanging around the place if I'm not there.  No matter what, when you connect, you should get a nice entry message welcoming you to the room.  As long as you see it, you know you connected to my PC, told me hello, and my PC replied back to you.

Enjoy guys!  I don't know how much more I'll continue to work with this little demo, but I think it already has proven beyond a doubt that QB64 can be used to communicate across the internet for us.  ;)
« Last Edit: November 20, 2019, 07:19:04 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Mini Messenger
« Reply #19 on: November 20, 2019, 01:21:41 am »
Steve

I got this virus threat warning when running your mini program (which I called X.exe)
Steve_chat_virus.PNG

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Mini Messenger
« Reply #20 on: November 20, 2019, 02:43:21 am »
Some suggestions :-
Screenshot (1317).png

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Mini Messenger
« Reply #21 on: November 20, 2019, 07:37:35 am »
Updated the code in the above post, due to user feedback.  The PONG message is now hidden by default (as it should have been, but wasn't).  Several people last night were asking, "What the heck is PONG anyway?"; here's your answer:

PING and PONG are IRC chat commands in which the host and client applications basically send a very minor amount of information back and forth to make certain that each other is still active and connected.  If the client fails to send a PING in time, the host will close their connection, free the handles, and release some memory.  If the host fails to send the PONG to the client in time, the client realizes the server has crashed, and terminates the session for the user.

It's basically just the two letting each other know they're still active and playing nicely together.  Both are 100% automated responses and not anything that the end user actually needs to worry about.  I simply had them visible for diagnostic purposes while coding the programs.   They're hidden by default now, so unless you just want to watch them spam back and forth, they should no longer be of any bother to you.  ;)

Many thanks for all the suggestions to improve the project guys; I really appreciate the interest in it.   There's just one thing here that I want to point out: I'm not out to recreate Discord, IRC, or a new chat program which will work across the internet.  I simply wanted to work up a nice demo to show that QB64 can work over TCP/IP and connect two computers across the world, and not just across a local network.  I think this demo does that for us, so I doubt I'll actually do much more work on it.

Many thanks to everyone who has tested the program out overnight (there's been 14 people so far!).  I doubt this is a program which I'll keep running 24/7 on my PC, but I will try and keep the host up and available for the next few days so everyone can test it out.  After that, it'll probably only be up "by request only", so if you want to test out the demo later, just post me a message, or send me an email, and I'll happily work with you to make it temporarily available once again.

Enjoy, one and all!  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Mini Messenger
« Reply #22 on: November 20, 2019, 11:49:29 am »
Steve, add a list of connected IP addresses to the side of your computer. You can then easily deny access for spammers. Another thing - what to try files transfer?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Mini Messenger
« Reply #23 on: November 21, 2019, 09:51:32 am »
The little mini-messenger chat server has been up and running for the last couple of days, and only keybone and myself have been visiting it.  It appears that the majority of everyone who is interested in this sort of thing has already tried it out by now, so I'm going to shut it down, close connections, and free up resources on my computer. 

If anyone is interested in connecting to the Mini Messenger in the future, just drop me a message and I'll pop it back online so you can play around with it and use it for testing anytime someone wants.  I just don't see any point in keeping it running 24/7 when it's just sitting there and doing nothing...  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Mini Messenger
« Reply #24 on: November 21, 2019, 12:05:51 pm »
  I just don't see any point in keeping it running 24/7 when it's just sitting there and doing nothing...  ;)

Imagine if everyone with web servers thought that way! The web would be a much smaller place. :(

Not that I fault your logic, tis a waste of electricity(unless your running Solar or Wind that gets pricey!), and wear and tear on equipment.

I thought it was a great example and totally awesome to see it in action.
Granted after becoming radioactive I only have a half-life!