R
robeng
Guest
I'm currently exploring networking and I'm creating a chat program for 2 users as a first project. I have set up everything I need except one thing. I want the server to send to my two clients a text string, the string with the chat history. My server has two variables names socket1 and socket2. Can I use these socket variables to send data to the clients or do I need to save their IP addresses?
I'm using TCP and my sockets are saved as socket1 and socket2.
This is what I have treid thus far. From my Async oServer event i send a packet to the Client. The client have a async event that should recive a string of the entire chat. But, when trying, the client does not recive any packets, and I'm not sure that the server even sends!
oServer Async event:
oClient Async event:
But, it won't work! Any help appreciated!
I'm using TCP and my sockets are saved as socket1 and socket2.
This is what I have treid thus far. From my Async oServer event i send a packet to the Client. The client have a async event that should recive a string of the entire chat. But, when trying, the client does not recive any packets, and I'm not sure that the server even sends!
oServer Async event:
Code:
/// @description Check for clients and data
var type_event = async_load[? "type"];
switch (type_event) {
case network_type_connect:
//Add the clients to the socket variable
if (socket1 == noone) {
scoket1 = async_load[? "socket"];
}
else if (socket2 == noone) {
socket2 = async_load[? "socket"];
}
break;
case network_type_disconnect:
//Remove the clients from the socket variable
socket1 = noone;
socket2 = noone;
break;
case network_type_data:
//Handle the data
var buffer = async_load[? "buffer"];
buffer_seek(buffer, buffer_seek_start, 0);
//Handle data function ^^
var messageName = buffer_read(buffer, buffer_string);
var messageText = buffer_read(buffer, buffer_string);
text += (messageName + ": " + messageText + "\n");
oText.text = text;
//Send packet to clients
buffer = buffer_create(1024, buffer_grow, 1);
buffer_write(buffer, buffer_string, text);
network_send_packet(socket1, buffer, buffer_tell(buffer));
break;
}
Code:
/// @description Check for server data
var type_event = async_load[? "type"];
switch (type_event) {
case network_type_data:
//Handle the data
var buff = async_load[? "buffer"];
buffer_seek(buff, buffer_seek_start, 0);
//Handle data function ^^
var messageText = buffer_read(buff, buffer_string);
oText.text = messageText;
break;
}
Last edited by a moderator: