วันเสาร์ที่ 8 พฤศจิกายน พ.ศ. 2557


Drupal 7
การใช้ Nodejs
- macbook pro : OS X Yosemit version 10.10
- Xammp 1.8.3-5

1.  เราก็ต้อง Download nodejs -> http://nodejs.org/download/ เลือกตาม platform ว่าจะลงบน platform อะไร
2. เราต้อง Download  Modules จาก https://www.drupal.org/project/nodejs  พอเรา Download ก็ทำการ install module
   2.1 พอเรา enable module แล้ว เราก็ไปที่ admin->config->nodejs->js  หา Service Key -> เราใส่ key อะไรก็ได้ที่เป้นของเราเป้นความลับนะ (เก็บไว้ดีๆ) กด save configuration  แล้วหา
Suggested configuration copy เอาไว้นะครับ  
  2.2 ไปที site/default/settings.php  และ $conf['nodejs_service_key'] = 'Service key ของคุณ จาก 2.1';
  2.3 ไปที่ site/all/modules/nodejs หาไฟล์ชือ nodejs.config.js ควร backup ไว้ก่อนนะ จาก 2.1 
Suggested configuration copy เราก็ past แล้ว ก็ save 
  2.4 เปิด terminal แล้ว cd ไป site/all/modules/nodejs แล้วก็ รัน  node server.js 
       *** ปัญหาต้องทีเกิดตอนผมทำ มัน error console log   : GET http://localhost:8888/socket.io/?EIO=3&transport=polling&t=1415505137132-4 400 (Bad Request) 

           การแก้ปัญหา คือ เปิด ไฟล์ nodejs.config.js แล้วหา transports แล้วใส่ "polling" 

Ex.
  "transports":["websocket",
  "flashsocket",
  "htmlfile",
  "xhr-polling",
  "jsonp-polling",
  "polling"],

แล้วเราปิด terminal แล้วเปิด Terminal ใหม่ แล้วรัน node server.js ตอนนี้มันทำงานได้ ละ ^^

การใช้งาน 
    1. เปิด Browser ลอง localhost/XXXXXX  แล้วดู terminal มันจะมี 
publishMessageToContentChannel: The channel "watchdog_dblog" doesn't exist.
publishMessageToContentChannel: message { channel: 'watchdog_dblog',
  commands:
   [ { command: 'insert',
       method: 'before',
       selector: '#admin-dblog tr:eq(1)',
       data: '<tr class="dblog-page-not-found dblog-warning"><td class="icon"></td><td>page not found</td><td>11/09/2014 - 11:00</td><td>themes/bartik/foo.js</td><td><span class="username" xml:lang="" typeof="sioc:UserAccount" property="foaf:name" datatype="">Anonymous (not verified)</span></td><td></td></tr>',
       settings: null } ],
  callback: 'nodejsWatchdog',
  clientSocketId: '' }
publishMessageToContentChannel: The channel "watchdog_dblog" doesn't exist.
publishMessageToContentChannel: message { channel: 'watchdog_dblog',
  commands:
   [ { command: 'insert',
       method: 'before',
       selector: '#admin-dblog tr:eq(1)',
       data: '<tr class="dblog-cron dblog-notice"><td class="icon"></td><td>cron</td><td>11/09/2014 - 11:00</td><td>Cron run completed.</td><td><span class="username" xml:lang="" typeof="sioc:UserAccount" property="foaf:name" datatype="">Anonymous (not verified)</span></td><td></td></tr>',
       settings: null } ],
  callback: 'nodejsWatchdog',
  clientSocketId: '' }
publishMessageToContentChannel: The channel "watchdog_dblog" doesn't exist.



  2. ลองหา module drupal มาลองดูนะว่ามันทำงานได้ จริงเปล่า ==> https://github.com/rh0/swapit_demo Download แล้วก็เปิดมันขึ้นมาลองเข้า ไป ที่ http://localhost/swap แล้วกรอบข้อความ Node message แล้วกด swap that header ข้อความจะ update 


 ปล. ลอง Debug Javascript เล่นมันดูครับผมว่าเว็บจะ dynamic มากกกกกกก


เรียบร้อยครับ