In previous articles, we talked about the function of using Websocket to call remote methods. On this basis, we can simply use WebSocket for data processing applications; you only need to execute relevant database operations in the method and return the result. It is very easy to use the rich controls provided by the jeasyui library for data application processing. the following uses jeasyui and WebSocket to query the Northwind Data Order.
First, analyze the logic functions required to query the case with the next order.
The functions shown in the preceding example are as follows:
1) Employee Query
2) Customer Query
3) query orders by PAGE
4) Order details Query
C # code
Simple logical query can be implemented based on the above functions. The Code is as follows:
public class Handler { public IList<BLEmployee> ListEmployees() { Console.WriteLine("List Employees"); IList<BLEmployee> items = new Expression().List<Employee, BLEmployee>(); return items; } public IList<BLCustomer> ListCustomers() { Console.WriteLine("List Customers"); IList<BLCustomer> items = new Expression().List<Customer, BLCustomer>(); return items; } public IList<BLOrderDetail> GetOrderDetail(int orderid) { Console.WriteLine("GetOrderDetail OrderID:{0}", orderid); return (Order.orderID == orderid).List<OrderDetail, BLOrderDetail>(); } public OrderSearchResult ListOrder(OrdersSearch search) { Console.WriteLine("ListOrder Employee:{0}\t Customer:{1}", search.EmployeeID, search.CustomerID); OrderSearchResult result = new OrderSearchResult(); Expression exp = new Expression(); if (!string.IsNullOrEmpty(search.CustomerID)) exp &= Customer.customerID.At() == search.CustomerID; if (search.EmployeeID > 0) exp &= Employee.employeeID.At() == search.EmployeeID; int count = exp.Count<Order>(); result.Orders = exp.List<Order, BLOrder>(new Region(search.Page, search.PageSize)); result.Count = count; return result; } }
With the Open Source component Smark. Data, you only need to write simple code to implement the corresponding Data query logic processing.
JavaScript
Define websocket connection:
// Create the connection function connect () {channel = new TcpChannel (); channel. connected = function (evt) {$ ('# dlgConnect '). dialog ('close'); setTimeout (listEmployee, 50); setTimeout (listCustomer, 50); listOrder () ;}; channel. disposed = function (evt) {$ ('# dlgConnect '). dialog ('open') ;}; channel. error = function (evt) {alert (evt) ;}; channel. connect ($ ('# txtHost '). val ());}
Data Query is performed in the connection creation completion event. Sending failed due to the continuous sending of more than 3 websocket requests. The specific reason is unknown. therefore, you can only use setTimeout to control the initialized data query. after the connection is created, you can call the remote method. The specific code for data query is as follows:
Employee query Remote Call method:
// Query var callListEmployees = {url: 'handler. listEmployees ', parameters :{}}; function listEmployee () {channel. send (callListEmployees, function (result) {result. data. unshift ({EmployeeID: 0, FullName: 'null'}); $ ('# employees '). combobox ({data: result. data, valueField: 'employeeid', textField: 'fullname '});});}
Customer query Remote Call method:
// The customer queries var callListCustomers = {url: 'handler. listCustomers ', parameters :{}}; function listCustomer () {channel. send (callListCustomers, function (result) {result. data. unshift ({CustomerID: '', CompanyName: 'null'}); $ ('# MERs '). combobox ({data: result. data, valueField: 'mermerid', textField: 'companyname '});});}
Order query Remote Call method:
// Order query var callListOrder = {url: 'handler. listOrder ', parameters: {search: {EmployeeID: 0, CustomerID: null, Page: 0, PageSize: 10 }}; function listOrder () {channel. send (callListOrder, function (result) {$ ('# orders '). datagrid ('loaddata', result. data. orders); $ ('# pp '). pagination ('refresh', {total: result. data. count });});}
Order details Remote Call method:
// Order details var callGetOrderDetail = {url: 'handler. getOrderDetail ', parameters: {orderid: 0 }}; function getOrderDetail () {channel. send (callGetOrderDetail, function (result) {$ ('# gdOrderDetail '). datagrid ('loaddata', result. data );});}
Summary
You only need the simple code above to implement an order query function. It looks similar to the traditional ajax application, and the service used here is based on websocket instead of webserver.
Download Code: WebSocket.Northwind.rar (1.03 mb)
Demo address: http://html5.ikende.com/northwind.htm