Click here to Skip to main content
11,637,176 members (69,701 online)
Click here to Skip to main content
Add your own
alternative version

A JavaScript Shell

, 18 Nov 2010 CPOL 14.6K 535 26
Bases to create your own JavaScript shell for your application
binaries.zip
binaries
Documentation.chm
Scaredfinger.JSShell.dll
samples.zip
samples
LocalShell
Properties
SampleShell
Properties
Scaredfinger.JSShell
Properties
ShellServer
Properties
source.zip
source
Scaredfinger.JSShell
Properties
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)

Share

About the Author

Erich Ledesma
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 18 Nov 2010
Article Copyright 2010 by Erich Ledesma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid