Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var handler = new HttpHandler();
            handler.AddStatic("^/$", "./www/index.html");
            handler.AddStatic("^/script/(.*)$", "./www/script/{1}");
            handler.AddStatic("^/file/(.*)$", "./www/test/file/{1}");

            handler.AddDynamic("^/test.web$", TestModule.TestController.TestMain);
            handler.AddWebSocket("^/echo$", strings => new WebSocketHandler());
            handler.AddJSON("^/test.json$", strings =>
                                                {
                                                    dynamic json = new JObject();
                                                    json.test = "abcd";
                                                    json.test2 = 123;
                                                    json.a = new JArray(strings);
                                                    return json;
                                                });
            var io = new SocketIO();
            io.On("connection", s =>
                                    {
                                        var socket = s as ISocketIO;
                                        socket.Emit("news", new JArray(JsonConvert.DeserializeObject("{ hello: 'world' }")));
                                        socket.On("my other event", array => System.Console.WriteLine(array[0].my));
                                    });
            handler.AddSocketIO(io);

            var netronics = new Netronics.Netronics(new HttpProperties(() => handler));
            netronics.Start();
            ExitEvent.WaitOne();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
            };

            var handler = new HttpHandler();
            handler.AddStatic("^/$", "./www/index.html");
            handler.AddStatic("^(.*).css$", "./www/{1}.css");
            handler.AddStatic("^/script/(.*)$", "./www/script/{1}");
            handler.AddStatic("^/file/(.*)$", "./www/test/file/{1}");

            handler.AddDynamic("^/test.web$", TestModule.TestController.TestMain);
            handler.AddWebSocket("^/echo$", strings => new WebSocketHandler());
            handler.AddJSON("^/test.json$", strings =>
                                                {
                                                    dynamic json = new JObject();
                                                    json.test = "abcd";
                                                    json.test2 = 123;
                                                    json.a = new JArray(strings);
                                                    return json;
                                                });
            var io = new SocketIO();
            io.On("connection", s =>
                                    {
                                        var socket = s as ISocketIO;
                                        lock (UserList)
                                        {
                                            UserList.Add(socket);

                                            dynamic obj = new JObject();
                                            obj.name = "System";
                                            obj.msg = socket.ToString() + " 접속";
                                            foreach (var sock in UserList)
                                            {
                                                sock.Emit("chat", new JArray(new object[] { obj }));
                                            }
                                        }

                                        socket.On("chat", o =>
                                                              {
                                                                  dynamic obj = new JObject();
                                                                  obj.name = socket.ToString();
                                                                  obj.msg = o[0].msg;
                                                                  lock (UserList)
                                                                  {
                                                                      foreach (var sock in UserList)
                                                                      {
                                                                          sock.Emit("chat", new JArray(new object[] { obj }));
                                                                      }
                                                                  }
                                                              });
                                    });
            io.On("disconnect", o =>
                                    {
                                        var socket = o as ISocketIO;
                                        lock (UserList)
                                        {
                                            UserList.Remove(socket);

                                            dynamic obj = new JObject();
                                            obj.name = "System";
                                            obj.msg = socket.ToString() + " 접속 종료";
                                            foreach (var sock in UserList)
                                            {
                                                sock.Emit("chat", new JArray(new object[] { obj }));
                                            }
                                        }
                                    });
            handler.AddSocketIO(io);

            var netronics = new Netronics.Netronics(new HttpsProperties(() => handler));
            netronics.Start();
            ExitEvent.WaitOne();
        }