IncomingNTM
Skip to content
mnscript
IncomingNTM(number, NTM) 

Description ​

Invoked when an incoming network message is received

Arguments ​

number - The incoming message's protocol
NTM - The NTM message received

Example ​

msc
using System;
using Console;
using Net;
using Event;

// This will be ran when a message is received
function OnNetworkMessageReceived(number protocol, NTM message){
    // We know that the protocol we are using is 6000.
    // We don't want to read data from other network messages.
    if(protocol != 6000){
        return;
    }

    string sourceAddress = message.GetSourceAddress();

    // We must read the data in the same order it was written.
    string text = message.ReadString();
    bool value = message.ReadBool();

    Console.WriteLine("Message from ["..sourceAddress.."]: "..text);
}

Event.AddListener("IncomingNTM", "incoming_ntm", "OnNetworkMessageReceived");

// Sending a network message to our own terminal, basically like localhost.
// If you put the IP of a different terminal here, it would be sent there instead.
string myIp = System.GetIP();

Net.Start(6000);
Net.WriteString("Hello!");
Net.WriteBool(true);
Net.SendToAddress(myIp);

// Continue processing events forever.
while(true){
    Event.Process();
}