code-prettify

2015年7月16日 星期四

ASP.NET API 加入 JSONP 支援

使用 Nuget 安裝 WebApiContrib.Formatting.Jsonp 套件

在 Global.asax 的 Application_Start 函式中加入

GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));

在 App_Start/WebApiConfig.cs 的 Register 修改 Route 如下

config.Routes.MapHttpRoute(
   name: "DefaultApi",
   routeTemplate: "api/{controller}/{id}/{format}",
   defaults: new
   {
       id = RouteParameter.Optional,
       format = RouteParameter.Optional
   }
);

這樣就可以支援 Jsonp!

註:會自動判斷 Headers 的 Accept 參數,如果是 application/json 則會回傳 json 格式

資料來源:
WebApiContrib.Formatting.Jsonp
https://github.com/WebApiContrib/WebApiContrib.Formatting.Jsonp

2015年7月15日 星期三

Qt WebSocket

宣告 WebSocketServer
new QWebSocketServer("Web Server", QWebSocketServer::NonSecureMode, this)

使用 listen() 開始接受 client 連線

同樣是使用 newConnection() 接受新的 client 事件
使用 nextPendingConnection() 取得 client 的 socket

QWebSocket 使用 disconnected 斷線事件

因為 WebSocket 規範中有兩種資料傳送方式,純文字模式及二進制模式,
所以資料接收事件也分為兩種,分別為
textMessageReceived,參數為 QString message
binaryMessageReceived,參數為 QByteArray message

傳送資料也分為兩個函式,分別為
sendTextMessage
sendBinaryMessage

簡單範例

connect(m_pWebSocketServer, &QWebSocketServer::newConnection, [=]() {
qDebug() << "new client socket";

QWebSocket* socket = this->m_pWebSocketServer->nextPendingConnection();

connect(socket, &QWebSocket::disconnected, [=]() {
qDebug() << "client socket disconnected";

socket->deleteLater();
});

connect(socket, &QWebSocket::textMessageReceived, [=](QString message) {
qDebug() << "client textMessageReceived";

qDebug() << "Read: " << message;
qDebug() << "Send " << message;

socket->sendTextMessage(message);
socket->close();
});

connect(socket, &QWebSocket::binaryMessageReceived, [=](QByteArray message) {
qDebug() << "client processBinaryMessage";

qDebug() << "Read: " << message;
qDebug() << "Send " << message;

socket->sendBinaryMessage(message);
socket->close();
});
});

m_pWebSocketServer->listen(QHostAddress::Any, 8812);

資料來源
Echo Server Example
http://doc.qt.io/qt-5/qtwebsockets-echoserver-example.html

QWebSocketServer
http://doc.qt.io/qt-5/qwebsocketserver.html

2015年7月14日 星期二

Qt TcpSocket

Server 的部份使用 QTcpServer

呼叫 listen 函式開始接受 Client 連線,參數為 QHostAddress::Any, 8811

在 newConnection signal 接受 socket

QTcpSocket* socket = server->nextPendingConnection();

在 readyRead() signal 接收資料

QBtyeArray data = socket->readyAll()

使用 write() 傳送資料

簡單的範例:

  m_server = new QTcpServer();
connect(m_server, &QTcpServer::newConnection, [=]() { qDebug() << "new client socket"; QTcpSocket* socket = this->m_server->nextPendingConnection(); connect(socket, &QTcpSocket::disconnected, [=]() { qDebug() << "client socket disconnected"; socket->deleteLater(); }); connect(socket, &QTcpSocket::readyRead, [=]() { qDebug() << "client readyRead"; QString string = socket->readAll(); qDebug() << "Read: " << string; string = string.toUpper(); qDebug() << "Send " << string; socket->write(string.toLatin1()); socket->close(); }); }); m_server->listen(QHostAddress::Any, 8811); qDebug() << "server listen";


Client 直接使用 QTcpSocket

呼叮 connectToHost 與 server 連線
連上了會觸發 connected 訊號,斷線則是 disconnected
接收資料一樣是 readyRead

簡單的範例:

m_client = new QTcpSocket();

connect(m_client, &QTcpSocket::connected, [=]() {
qDebug() << "socket connected";

QString string = "Hi";
m_client->write(string.toLatin1());
qDebug() << "Send: " << string;
});

connect(m_client, &QTcpSocket::disconnected, [=]() {
qDebug() << "socket disconnected";
});

connect(m_client, &QTcpSocket::readyRead, [=]() {
qDebug() << "client readyRead";

QString string = m_client->readAll();
qDebug() << "Read: " << string;

m_client->close();
});

m_client->connectToHost("127.0.0.1", 8811);


資料來源:
Network Programming with Qt
http://doc.qt.io/qt-5/qtnetwork-programming.html

Sockets - Server & Client using QT
http://www.bogotobogo.com/cplusplus/sockets_server_client_QT.php

QTcpSocket Class
http://doc.qt.io/qt-5/qtcpsocket.html

2015年6月24日 星期三

複製所有檔案及資料夾指令

Windwos

xcopy source [Destination] /i /e /k /h

Linux

cp -R source dest

資料來源

Xcopy
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx

xcopy functionality with linux?
http://www.linuxquestions.org/questions/linux-general-1/xcopy-functionality-with-linux-386643/

2015年6月17日 星期三

如何將 Sublime Text 3 加入右鍵選單中

從 Notepad++ 改用 Sublime Text 之後,最不習慣的地方就是在右鍵選單內,
將下列內容存成 run.bat 後,以管理員的權限執行,就會將 Sublime Text 加入右鍵選單

 @echo off
  SET st2Path=C:\Program Files\Sublime Text 3\sublime_text.exe

  rem add it for all file types
  @reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3"         /t REG_SZ /v "" /d "Open with Sublime Text 3"   /f
  @reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3"         /t REG_EXPAND_SZ /v "Icon" /d "%st2Path%,0" /f
  @reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st2Path% "%%1"" /f

  rem add it for folders
  @reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3"         /t REG_SZ /v "" /d "Open with Sublime Text 3"   /f
  @reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3"         /t REG_EXPAND_SZ /v "Icon" /d "%st2Path%,0" /f
  @reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st2Path% "%%1"" /f
  pause

資料來源
Add “Open with Sublime Text 3″ to Windows Explorer Context Menu
http://wordpress-corner.com/add-open-sublime-text-3-windows-explorer-context-menu/

2015年6月15日 星期一

Sevabot - Skype bot - Windows Install

一、從 git 取得 Sevabot

git clone git://github.com/opensourcehacker/sevabot.git


二、安裝設定

在 cmd 輸入下列指令

cd sevabot
powershell -NoProfile -ExecutionPolicy unrestricted -Command "add-content -path virtualenv.py -value(new-object net.webclient).DownloadString('https://raw.github.com/pypa/virtualenv/master/virtualenv.py')"
python virtualenv.py venv --no-setuptools

set-executionpolicy unrestricted
    . .\venv\Scripts\activate.ps1
這段意思似乎是將 activate.ps1 設定為「可執行」的樣子
具體行為不知道是什麼?所以我什麼也沒做

python setup.py develop

執行後在 python 目錄的 Scripts 資料夾 (C:\Python27\Scripts) 找到 sevabot.exe 及 sevabot-script.py,複製到 sevabot 目錄下。

接著從範本複製一份設定出來。
cp settings.py.example settings.py

最後執行 sevabot

sevabot.exe

這時候 skype 會出現第三方元件存取的詢問訊息,記得按允許,
同時會啟動 web service,預設在 localhost:5000

三、使用 sevabot 傳送訊息

啟動 browser 開啟 localhost:5000 會看到網頁



其中 Shared Secret 就在 settings.py 中的 SHARED_SECRET 設定 (圖中為 koskela)
之後使用 web api 傳送訊息的時候都要使用這組密碼


使用 POST 時,需要參數 shared_secret 及 message
使用 Postman 範例


使用 cURL 範例

curl -F "shared_secret=koskela" -F "message=Hi, This is test." http://localhost:5000/message/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

資料來源

Sevabot - Skype bot 1.0 documentation
https://sevabot-skype-bot.readthedocs.org/en/latest/

Github - opensourcehacker/sevabot
https://github.com/opensourcehacker/sevabot

Installation fails on Windows: Pip and setuptools fail to install #94
https://github.com/opensourcehacker/sevabot/issues/94