Neo Chat (BNet/MaxGui Powered)

BlitzMax Forums/BlitzMax Programming/Neo Chat (BNet/MaxGui Powered)

AntonyWells(Posted 2006) [#1]
You can download a demo for Blitz Net Optica here. It's a multi-user chat program wrote in bmax with maxgui for the interface and BlitzNet Optica for the network. it includes bins so you need neither module to run it.

http://dspace.dreamora.net/


Includes both server and client. You can create a server and connect as many clients locally as you like for testing. I'd be grateful if someone could host a server for a few hours and post their i.p. I can't because of my router.

Edit- Thank dreamora for the webspace. :)


AntonyWells(Posted 2006) [#2]
Server Source

Strict
Import "Network.bmx"


Local server:TServer = TServer.Create(5555 )

Local player:TClass = Tclass.create("Chatter")
player.AddString("Txt",500,True)

Local nl:TLogger = Tlogger.Create("Server")

server.addClassTemplate( player )

Repeat

	nl.update()
	server.update()
	
	Delay 20
Forever



Client source

Strict
Import "Network.bmx"

Local ip$=Input("Network I.P(Leave blank For 127.0.0.1)")
If ip=""
	ip="127.0.0.1"
EndIf
Global name:String = Input("Name>")
Local nl:TLogger = Tlogger.Create(name)

'Set up client.
Global client:TClient = TClient.Create( name,ip,5555 )

'Create player class template
Local Chatter:TClass = Tclass.create("Chatter")
Chatter.addString("Txt",500,True)

Client.AddClassTemplate( chatter )


'---Wait till connected
SeedRnd( MilliSecs() )


'Register Event call back

client.RegisterEventCallBack( New ChatCallBack )

'---Create Event callback class.
Type ChatCallBack Extends NEventCallBack
	Method OnNewClass( event:NEvent )
		
		Select event.own.class
			Case "Chatter"
				Print "Connected "+event.own.name
				Local u:User = CreateUser( event.own.name,event.own )
				event.own.setData( u )
				TChatGui.AddUser( u )
				
		End Select
	
	End Method
	
	Method OnDeleteClass( event:NEvent )
		netlog.log "On delete called."
		
		Select event.own.class
			Case "Chatter"
				
				Local u:user = USer( event.own.getData() )
				users.remove( u )
				Print event.own.name+" Disconnected"
				TChatGui.RemoveUser( u )
				TChatGui.Log( u.name+" Disconnected" )
		End Select
	
	End Method
	
	Method OnConnect( event:NEvent )

		Print "Connected.."
		Local useNet:TClass = client.SpawnClass("Chatter",name )
		LocalUser = CreateUser( name,useNet )
		useNet.SetData( Object( LocalUser ) )
		TChatGui.AddUser( localUser )
				
	End Method
	
	Method OnUpdateVar( event:NEvent )
	
		Select event.own.class

			Case "Chatter"
				
				Local u:user = User( event.own.getData() )
				Select event.varo.name
					Case "Txt"
						
						TChatGui.Log( event.own.name+":"+event.varo.str )
												
				End Select
		
		End Select
		
	End Method

End Type


'Bot List and Local Player object
Global Users:Tlist = CreateList()
Global LocalUser:User

Type User
	
	Field net:TClass
	Field name:String
	
End Type

Type TChatGUI
	Function AddUser(u:User)
	'	AddGadgetItem UserView,u.name,0,-1,"BlitzNet Chat user")
		Users.addlast( u )
		BuildUserList()
	End Function
	
	Function RemoveUser(u:User)	
		users.remove( u )
		BuildUserList()
	End Function
	
	Function BuildUserList()
		Local gi = CountGadgetItems( UserView )
		For Local j=0 Until gi
			RemoveGadgetItem userview,j
		Next
		For Local u:User = EachIn users
			AddGadgetItem userview,u.name,0,-1,"BlitzNet Chat User:"+u.name
		Next
	End Function
	
	
	Global Users:Tlist
	
	Function CreateGui()
		users = CreateList()
		win = CreateWindow("BlitzNet Optica Chatter (c)Freeware",20,20,700,450,Null,Window_TitleBar )
		ChatView = CreateTextArea(5,5,550,350,win,TEXTAREA_WORDWRAP|TEXTAREA_READONLY)
		UserView = CreateListBox( 560,5,130,350,win )
		UserChat = CreateTextField( 5,360,550,25,win )
		ChatSend = CreateButton("Send", 560,360,40,20,win,BUTTON_OK )
		Disconnect = CreateButton("Disconnect",605,360,60,20,win)
		Log("BlitzNet Optica Chatter. Give Me money Ware.")
	End Function
	
	Function Log( txt:String )
		
		Local mbuf:Byte Ptr = MemAlloc( Len(txt)+1 )
		Local mstr:tramstream = CreateRamStream( mbuf,Len(txt)+1,True,True )
		WriteLine mstr,txt
		Local ntxt:String = ""
		For Local j=0 Until Len(txt)+1
			ntxt:+Chr( mbuf[j] )
		Next
		AddTextAreaText chatView,ntxt
		MemFree mbuf
		CloseStream mstr
		
	End Function
	
	Function Update()
	
		WaitEvent()
		
		Select EventID()
			Case Event_GadgetAction
				Select EventSource()
					Case UserChat
					Case ChatSend
						SendChatter( TextFieldText(userChat) )
						SetGadgetText UserChat,""
					Case Disconnect
						Client.DeleteClass( LocalUser.Net )
						Log("Disconnected. Now in invisible mode.")
				End Select
		End Select
	
	End Function
	
	Function SendChatter( txt:String )
	
		Log( LocalUser.Name+":"+txt )
		LocalUser.Net.SetString( "Txt",txt,True )
			
	End Function
			
	Global Disconnect:TGadget
	Global ChatSend:TGadget
	Global UserChat:Tgadget 
	Global UserView:TGadget 
	Global ChatView:TGadget
	Global Win:TGadget 

End Type

TChatGui.CreateGui()
CreateTimer(25)

Repeat

	TChatGui.Update()
	client.update()
	client.ClearEvents()

Until KeyDown(KEY_ESCAPE)
	
Function CreateUser:User( name:String,class:TClass )
	
	Local b:user = New user
	b.net = class
	b.name = name
	users.addlast( b )
	Return b
	
End Function





SillyPutty(Posted 2006) [#3]
Awesome, now at least I can test this puppy you dig ?

Also, do you know yet if you are you going to actively maintain this module ?


AntonyWells(Posted 2006) [#4]
Definitely, I'll be using it in FMC. (See evak's worklog for details on that beauty :) )


ozak(Posted 2006) [#5]
Does Optica work with OSX and Linux too btw?


popcade(Posted 2006) [#6]
Well... when will registered users receive updates?

It's about 3 days since I see the version 1.2 info..


Kanati(Posted 2006) [#7]
I'll second that...


AntonyWells(Posted 2006) [#8]
ozak, yes all three platforms.

The update is not finished yet. It was when I updated my account but then things changed. I'll not forgot anyone, don't worry.