Jump to content

Server:Broadcast() does not send stream


Recommended Posts

I don't think networking is done is it? Last I checked it doesn't deal with endianness so a big/little endian machine can't communicate with eachother.... this can't be done manually through Lua sadly. (Or at least easily)

But it'd really help if you could post your code! I was playing with this not long ago and it seems to work fine... Are you sure the message is being both sent and recieved? Server::Update and Client::Update both return NULL when there is no message.

Link to comment
Share on other sites

 

This is an example using Josh's chat client code.

 

--------------------------------------------------------
--Function to build user interface on each window
--------------------------------------------------------
function BuildGUI(context)
	local interface = {}
	interface.gui=GUI:Create(context)
	interface.root = interface.gui:GetBase()
	interface.root:SetScript("Scripts/GUI/Panel.lua")
	interface.chatlog = Widget:TextArea(4,4,interface.root:GetClientSize().x-8,interface.root:GetClientSize().y-30-8,interface.root)
	interface.textbox = Widget:TextField("",4,interface.root:GetClientSize().y-30,interface.root:GetClientSize().x-8-72-4,26,interface.root)
	interface.sendbutton = Widget:Button("Send",interface.root:GetClientSize().x-4-72,interface.root:GetClientSize().y-30,72,26,interface.root)
	return interface
end

--------------------------------------------------------
--Initialize client and server
--------------------------------------------------------
local MESSAGE_CHAT=1
local peer = nil
local port=8888
local client = Client:Create()
local server = Server:Create(port)
local success = client:Connect("127.0.0.1",port)

--------------------------------------------------------
--Create server interface
--------------------------------------------------------
serverwindow=Window:Create("Chat Server",0,0,400,300,Window.Titlebar)
--servercontext=Context:Create(serverwindow)
local serverinterface = BuildGUI(serverwindow)

--------------------------------------------------------
--Create client interface
--------------------------------------------------------
clientwindow=Window:Create("Chat Client",300,100,400,300,Window.Titlebar)
--clientcontext=Context:Create(clientwindow)
local clientinterface = BuildGUI(clientwindow)

--------------------------------------------------------
--Main loop
--------------------------------------------------------
while true do
	
	--------------------------------------------------------
	--Exit the program
	--------------------------------------------------------
	if serverwindow:KeyHit(Key.Escape) or clientwindow:KeyHit(Key.Escape) then return end
	
	--------------------------------------------------------
	--Handle closed windows
	--------------------------------------------------------
	if serverwindow:GetHidden()==false then
		if serverwindow:Closed() then
			serverwindow:Hide()
			if clientwindow:Closed() then return end
			server:Disconnect(peer)
		end
	end
	
	if clientwindow:GetHidden()==false then
		if clientwindow:Closed() then
			clientwindow:Hide()
			client:Disconnect()
		end
	end
	
	--------------------------------------------------------
	--Update client
	--------------------------------------------------------
	while true do
		local message = client:Update()
		if message==nil then break end
		
		if message.id==Message.Connect then
			clientinterface.chatlog:AddText(">> Connected to server.")
			
		elseif message.id==Message.Disconnect then
			clientinterface.chatlog:AddText(">> Disconnected from server.")
			
		elseif message.id==MESSAGE_CHAT then
			System:Print("Server: "..tostring(message.stream:ReadString()))
			message.stream:Seek(0)
			clientinterface.chatlog:AddText("Server: "..tostring(message.stream:ReadString()))
			
		end
		message:Release()
	end
	
	--------------------------------------------------------
	--Update server
	--------------------------------------------------------
	while true do
		local message = server:Update()
		if message==nil then break end
		
		if message.id==Message.Connect then
			serverinterface.chatlog:AddText(">> New client connected.")
			peer = message.peer
			
		elseif message.id==Message.Disconnect then
			serverinterface.chatlog:AddText(">> Client disconnected.")
			
		elseif message.id==MESSAGE_CHAT then
			serverinterface.chatlog:AddText("Client: "..tostring(message.stream:ReadString()))
			
		end
		message:Release()
	end
	
	--------------------------------------------------------
	--Handle GUI events
	--------------------------------------------------------
	while EventQueue:Peek() do
		local event = EventQueue:Wait()
		
		if event.id == Event.WidgetAction then
			
			if event.source == clientinterface.sendbutton or event.source==clientinterface.textbox then
				local s = clientinterface.textbox:GetText()
				client:Send(MESSAGE_CHAT,s)
				clientinterface.chatlog:AddText("Me: "..s)
				clientinterface.textbox:SetText("")
				clientinterface.gui:SetFocus(clientinterface.textbox)
			end
			
			if event.source == serverinterface.sendbutton or event.source==serverinterface.textbox then
				local s = serverinterface.textbox:GetText()
				--server:Send(peer,MESSAGE_CHAT,s)				
				server:Broadcast(MESSAGE_CHAT,s,0,Message.Reliable)
				serverinterface.chatlog:AddText("Me: "..s)
				serverinterface.textbox:SetText("")
				serverinterface.gui:SetFocus(serverinterface.textbox)
			end
		
		end
	end
	
	--------------------------------------------------------
	--Refresh both contexts
	--------------------------------------------------------
	--servercontext:Sync()
	--clientcontext:Sync()

end

 

Ctrl+F Broadcast

This will work if you send from client to server, but the client will not ever receive data. If the server send no text at all, and you just click send, it will crash.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...