php socket communication help

BlitzMax Forums/BlitzMax Programming/php socket communication help

JoshK(Posted 2009) [#1]
This code adds a server to a central list of available servers. The program must refresh itself on the server or it will be automatically removed after five minutes.

First I tried opening a stream each time I needed to refresh. This causes a delay as the stream is opened. I want to be able to just leave the stream open and write a line to make it refresh every four minutes or so. How can I do this?:
SuperStrict

Import brl.stream
Import brl.socket
Import brl.httpstream

Type TServer
	
	'Field url:String
	Field stream:TStream
	
	Method Delete()
		If stream
			stream.WriteLine("?action=removeserver")
			stream.close()
		EndIf
		'Local stream:TStream
		'If url
			'stream=OpenStream(url+"?action=removeserver")
			'If stream stream.close()
		'EndIf
	EndMethod
	
	Method Refresh:Int()
		'Local stream:TStream
		'stream=OpenStream(url+"?action=refreshserver")
		'If Not stream Return False
		'stream.close()
		stream.WriteLine("?action=refreshserver")
		Return True	
	EndMethod
	
	Function Create:TServer(url:String,name$="",game$="")
		Local server:TServer=New TServer
		'server.url=url
		server.stream=OpenStream(url+"?action=addserver&gamename="+game+"&servername="+name)
		If Not server.stream Return Null
		While Not server.stream.Eof()
			If server.stream.ReadLine().Trim()
				server.stream.close()
				Return Null
			EndIf
		Wend
		Return server
	EndFunction
	
	Function Request:String[](url:String,game:String="")
		Local stream:TStream
		Local s$,sarr$[],n:Int
		stream=OpenStream(url+"?action=listservers&gamename="+game)
		If Not stream Return Null
		While Not stream.Eof()
			s=ReadLine(stream)
			If s.Trim().length>0
				sarr=sarr[..sarr.length+1]
				sarr[sarr.length-1]=s
			EndIf
		Wend
		stream.close()
		Return sarr
	EndFunction
	
EndType

Rem
bbdoc:Requests a list of available servers
EndRem
Function RequestServers:String[](url:String,game:String="")
	Return TServer.Request(url,game)
EndFunction



explosive(Posted 2009) [#2]
I'm afraid you can't do it at all, because HTTP (which is used by PHP) is a closed connection. This means you ask the server for data, the server sends it to you and then quits the connection. Everytime you need new data, you hav to establish a new connection.

The only possibility I can see is to run you own server. But don't know if there's any provider out there who let's you run your own programme.


JoshK(Posted 2009) [#3]
I see.


TartanTangerine (was Indiepath)(Posted 2009) [#4]
*any* dedicated server provider will let you run your own code. http://www.mosso.com have an entry level cloudserver for $10 per month. A colleague of mine pointed out today that you could compile a BMAX (non-windowed) executable and leave it running on the Linux Server.


plash(Posted 2009) [#5]
@Indiepath: I don't think halo would ever touch Linux.


TartanTangerine (was Indiepath)(Posted 2009) [#6]
Amazon EC2 let you create Windows servers (in the cloud).