Working basic REST server
This commit is contained in:
		
							parent
							
								
									ac468d7516
								
							
						
					
					
						commit
						af631a9b24
					
				
					 1 changed files with 23 additions and 17 deletions
				
			
		| 
						 | 
					@ -23,7 +23,8 @@ using NWItemProperty = NWScript.NWScriptEngineStructure4;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using System.Threading;
 | 
					using System.Threading;
 | 
				
			||||||
using Grapevine.Server;
 | 
					using Grapevine.Server;
 | 
				
			||||||
 | 
					using System.IO;
 | 
				
			||||||
 | 
					using System.Net;
 | 
				
			||||||
namespace CLRScript
 | 
					namespace CLRScript
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    public partial class nwn2_ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram
 | 
					    public partial class nwn2_ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram
 | 
				
			||||||
| 
						 | 
					@ -49,26 +50,31 @@ namespace CLRScript
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
 | 
					        public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var server = new RESTServer();
 | 
					            server = new RESTServer("127.0.0.1", "8080", "http", "index.html", "C:\\nowhere", 1);
 | 
				
			||||||
            server.Start();
 | 
					            server.Start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            while (server.IsListening)
 | 
					            AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString("Start!", TALKVOLUME_TALK); });
 | 
				
			||||||
            {
 | 
					            DelayCommand(0.5f, Heartbeat);
 | 
				
			||||||
                Thread.Sleep(300);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            int Volume;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            MessageToSpeak = "Hello, world";
 | 
					 | 
				
			||||||
            Volume = TALKVOLUME_TALK;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString(MessageToSpeak, Volume); });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return DefaultReturnCode;
 | 
					            return DefaultReturnCode;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [NWScriptGlobal]
 | 
					        public void Heartbeat()
 | 
				
			||||||
        private string MessageToSpeak;
 | 
					        {
 | 
				
			||||||
 | 
					            ActionSpeakString("hb", TALKVOLUME_TALK);
 | 
				
			||||||
 | 
					            DelayCommand(0.5f, Heartbeat);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        RESTServer server;
 | 
				
			||||||
 | 
					        public sealed class MyResource : RESTResource
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            [RESTRoute]
 | 
				
			||||||
 | 
					            public void HandleAllGetRequests(HttpListenerContext context)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                SendTextResponse(context, "GET is a success!");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue