Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / JScript .NET

A JavaScript Shell

Rate me:
Please Sign up or sign in to vote.
4.25/5 (4 votes)
18 Nov 2010CPOL4 min read 30.7K   739   29  
Bases to create your own JavaScript shell for your application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using Scaredfinger.JSShell;
using System.IO;
using System.Timers;

namespace ShellServer
{
  class Program
  {
    static bool _inactive;
    static EventHandler<EventArgs> _handler;

    public static void InactiveListener(object sender, ElapsedEventArgs e)
    {
      if (_handler != null && ! _inactive)
        _handler(sender, e);

      _inactive = true;
    }

    static void Main(string[] args)
    {
      var server = new TcpListener(23); // Since this is kind of a telnet, if you have a telnet server, you should change this.
      server.Start();

      while (true) // Running until closed
      {
        var client = server.AcceptTcpClient();

        using (var shell = ShellFactory<SampleShell.SampleShell>.Default.CreateShell())
        {
          using (var output = new StreamWriter(client.GetStream()))
          {
            output.WriteLine("Welcom to JSShell...");
            output.Flush();

            using (var input = new StreamReader(client.GetStream()))
            {
              shell.Output = output;
              shell.OpenShell();
              _handler = shell.CreateEventHandler<EventArgs>(@"
  this.Print(""You are inactive and might be kick out any moment"");
");
              var timer = new Timer(5000);
              timer.Elapsed += InactiveListener;
              timer.Start();

              while (shell.Running)
              {
                try
                {
                  output.Write(" > ");
                  output.Flush();
                  shell.Eval(input.ReadLine());
                  output.Flush();
                  _inactive = false;
                }
                catch (Exception ex)
                {
                  output.WriteLine(ex);
                  output.Flush();
                }
              } // while
            } // using input
          } // using output
        } // using shell
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SunHotels
Spain Spain
I Received a Bachelor's Degree in Computer Science at the Mathematics and Computer Science Faculty, University of Havana, Cuba.

I mainly work in web applications using C# and some Javascript. Some very few times do some Java.

Comments and Discussions