Implemented basic chat system
This commit is contained in:
parent
ee233c52f0
commit
9145747e20
@ -39,7 +39,7 @@ int main (int argc, char ** argv)
|
|||||||
address.port = SERVER_PORT;
|
address.port = SERVER_PORT;
|
||||||
|
|
||||||
|
|
||||||
const int CHANNEL_COUNT = 1;
|
const int CHANNEL_COUNT = 2;
|
||||||
server = enet_host_create (&address, MAX_PLAYERS, CHANNEL_COUNT, 0, 0);
|
server = enet_host_create (&address, MAX_PLAYERS, CHANNEL_COUNT, 0, 0);
|
||||||
|
|
||||||
if (server == NULL)
|
if (server == NULL)
|
||||||
@ -67,7 +67,8 @@ int main (int argc, char ** argv)
|
|||||||
std::cout << "A packet of length " << event.packet -> dataLength << " containing "
|
std::cout << "A packet of length " << event.packet -> dataLength << " containing "
|
||||||
<< event.packet -> data << " was received from " << event.peer -> data << " on channel"
|
<< event.packet -> data << " was received from " << event.peer -> data << " on channel"
|
||||||
<< event.channelID << std::endl;
|
<< event.channelID << std::endl;
|
||||||
|
if (event.channelID == 0)
|
||||||
|
{
|
||||||
char c [1];
|
char c [1];
|
||||||
c[0] = event.packet->data[0];
|
c[0] = event.packet->data[0];
|
||||||
|
|
||||||
@ -89,10 +90,24 @@ int main (int argc, char ** argv)
|
|||||||
std::cout << "Invalid packet recieved!" <<std::endl;
|
std::cout << "Invalid packet recieved!" <<std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else if(event.channelID == 1)
|
||||||
|
{
|
||||||
|
int peer_id = event.peer -> incomingPeerID;
|
||||||
|
if(usernames[peer_id] != "")
|
||||||
|
{
|
||||||
|
std::string chat_message((char*)event.packet->data);
|
||||||
|
std::string resp = "<" + usernames[peer_id] + "> " + chat_message;
|
||||||
|
const char* data = resp.c_str();
|
||||||
|
|
||||||
|
std::cout << data << std::endl;
|
||||||
|
|
||||||
//Destroy packet now that it has been used
|
ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE);
|
||||||
enet_packet_destroy (event.packet);
|
enet_host_broadcast(server, 1, packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enet_packet_destroy (event.packet);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
|
Reference in New Issue
Block a user