Exemple #1
0
onion_connection_status websocket_example_cont(void *data, onion_websocket *ws, ssize_t data_ready_len){
	char tmp[256];
	if (data_ready_len>sizeof(tmp))
		data_ready_len=sizeof(tmp)-1;
	
	int len=onion_websocket_read(ws, tmp, data_ready_len);
	if (len<=0){
		ONION_ERROR("Error reading data: %d: %s (%d)", errno, strerror(errno), data_ready_len);
		return OCS_NEED_MORE_DATA;
	}
	tmp[len]=0;
	onion_websocket_printf(ws, "Echo: %s", tmp);
	
	ONION_INFO("Read from websocket: %d: %s", len, tmp);
	
	return OCS_NEED_MORE_DATA;
}
Exemple #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;
}