Beispiel #1
0
onion_connection_status ws_handler(void *priv, onion_request * req,
                                   onion_response * res) {
  onion_websocket *ws = onion_websocket_new(req, res);
  ws_status.connected++;
  ws_status.is_connected = (ws != NULL);

  if (ws_status.is_connected) {
    onion_websocket_set_callback(ws, ws_callback);
    return OCS_WEBSOCKET;
  }
  return OCS_NOT_IMPLEMENTED;
}
Beispiel #2
0
onion_connection_status websocket_example(void *data, onion_request *req, onion_response *res){
	onion_websocket *ws=onion_websocket_new(req, res);
	if (!ws){
		onion_response_write0(res,
			"<html><body><h1>Easy echo</h1><pre id=\"chat\"></pre>"
			" <script>\ninit = function(){\nmsg=document.getElementById('msg');\nmsg.focus();\n\nws=new WebSocket('ws://'+window.location.host);\nws.onmessage=function(ev){\n document.getElementById('chat').textContent+=ev.data+'\\n';\n};}\n"
			"window.addEventListener('load', init, false);\n</script>"
			"<input type=\"text\" id=\"msg\" onchange=\"javascript:ws.send(msg.value); msg.select(); msg.focus();\"/>\n"
			"</body></html>");
		
		
		return OCS_PROCESSED;
	}

	onion_websocket_printf(ws,"Hello from server. Write something to echo it");
	onion_websocket_set_callback(ws, websocket_example_cont);
	
	return OCS_WEBSOCKET;
}